graphile-settings
Version:
graphile settings
55 lines (54 loc) • 1.9 kB
JavaScript
import streamer from '@launchql/s3-streamer';
import uploadNames from '@launchql/upload-names';
export class Uploader {
opts;
streamerInstance;
constructor(opts) {
this.opts = opts;
const { bucketName, awsRegion, awsSecretKey, awsAccessKey, minioEndpoint } = this.opts;
this.streamerInstance = new streamer({
defaultBucket: bucketName,
awsRegion,
awsSecretKey,
awsAccessKey,
minioEndpoint,
});
}
async resolveUpload(upload, _args, _context, info) {
const { uploadPlugin: { tags, type } } = info;
const readStream = upload.createReadStream();
const { filename } = upload;
const rand = Math.random().toString(36).substring(2, 7) +
Math.random().toString(36).substring(2, 7);
const key = `${rand}-${uploadNames(filename)}`;
const result = await this.streamerInstance.upload({
readStream,
filename,
key,
bucket: this.opts.bucketName
});
const url = result.upload.Location;
const { contentType, magic: { charset } } = result;
const typ = type || tags.type;
const allowedMimes = tags.mime
? tags.mime.trim().split(',').map((a) => a.trim())
: typ === 'image'
? ['image/jpg', 'image/jpeg', 'image/png', 'image/svg+xml']
: [];
if (allowedMimes.length && !allowedMimes.includes(contentType)) {
throw new Error(`UPLOAD_MIMETYPE ${allowedMimes.join(',')}`);
}
switch (typ) {
case 'image':
case 'upload':
return {
filename,
mime: contentType,
url
};
case 'attachment':
default:
return url;
}
}
}