-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better content filtering (#569)
* feat: better content filtering * feat: keep multers3 for non asset/items * chore: lock * chore: logs
- Loading branch information
1 parent
6e89266
commit 8ed90d0
Showing
8 changed files
with
1,454 additions
and
162 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { ACL, ACLValues, isValidFileSize, uploadFile } from './s3' | ||
|
||
type File = Express.Multer.File | ||
|
||
export async function uploadRequestFiles( | ||
files: { [fieldname: string]: File[] } | File[], | ||
getFileKey: (file: File) => Promise<string>, | ||
options: Partial<{ acl: ACLValues; mimeTypes: string[] }> = {} | ||
): Promise<File[]> { | ||
// We don't care about the response type here, we're just awaiting on the promises | ||
const uploadPromises: Promise<Object>[] = [] | ||
const acl = options.acl || ACL.publicRead | ||
|
||
// Transform the files into an array of files | ||
// req.files can be either an object with: { [fieldName]: Express.MulterS3.File[] } or an array of Express.MulterS3.File[] | ||
// depending on which method you use, multer().array() or multer().fields(). | ||
// The field name is still accessible on each File | ||
// If you're using fields() with a maxCount bigger than 1, you might want to use the original req.files object to do any further processing | ||
files = Array.isArray(files) ? files : Object.values(files).flat() | ||
|
||
for (const file of files) { | ||
if ( | ||
!isValidFileSize(file.size) || | ||
!isValidMimeType(file.mimetype, options.mimeTypes) | ||
) { | ||
throw new Error( | ||
`Invalid file ${file.fieldname}. Check the file size and mimetype.` | ||
) | ||
} | ||
|
||
uploadPromises.push( | ||
getFileKey(file).then((hash) => uploadFile(hash, file.buffer, acl)) | ||
) | ||
} | ||
|
||
await Promise.all(uploadPromises) | ||
|
||
return files | ||
} | ||
|
||
function isValidMimeType( | ||
mimeTypeToCheck: string, | ||
validMimeTypes: string[] = [mimeTypeToCheck] | ||
) { | ||
return validMimeTypes | ||
.map((mimeType) => mimeType.toLowerCase()) | ||
.includes(mimeTypeToCheck.toLowerCase()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pull from 'pull-stream' | ||
import { MemoryDatastore } from 'interface-datastore' | ||
import CID from 'cids' | ||
const Importer = require('ipfs-unixfs-engine').Importer | ||
|
||
export type ContentServiceFile = { | ||
path: string | ||
content: Buffer | ||
size: number | ||
} | ||
|
||
export async function getCID(file: ContentServiceFile): Promise<string> { | ||
const importer = new Importer(new MemoryDatastore(), { onlyHash: true }) | ||
return new Promise<string>((resolve, reject) => { | ||
pull( | ||
pull.values([file]), | ||
pull.asyncMap((file: ContentServiceFile, cb: any) => { | ||
const data = { | ||
path: file.path, | ||
content: file.content, | ||
} | ||
cb(null, data) | ||
}), | ||
importer, | ||
pull.onEnd(() => { | ||
return importer.flush((err: any, content: any) => { | ||
if (err) { | ||
reject(err) | ||
} | ||
resolve(new CID(content).toBaseEncodedString()) | ||
}) | ||
}) | ||
) | ||
}) | ||
} |