public processImage (req, res, next) { const _finish = (data) => { res.set({"Content-Type": res.locals.image_headers["content-type"]}) .send(data); next(); }; if (!res.locals.image_size) { _finish(res.locals.image_body); } else if (res.locals.image_iscrop) { sharp(res.locals.image_body) .resize(res.locals.image_size, res.locals.image_size) .toBuffer() .then(_finish) .catch(next); } else { sharp(res.locals.image_body) .resize(res.locals.image_size, null) .withoutEnlargement(true) .toBuffer() .then(_finish) .catch(next); } }
const uploadPromises = THUMBNAILSIZES.map(async thumbDef => { const thumbFileName = `thumb_${fileName}_${thumbDef.name}.${contentType.substring(contentType.indexOf('/') + 1)}`; const thumbFilePath = join(workingDir, thumbFileName); const fileTransform = sharp(tmpFilePath); if (object.contentType === 'image/jpeg') { fileTransform.jpeg({ quality: thumbDef.quality }); } if (object.contentType === 'image/png') { fileTransform.png({ quality: thumbDef.quality }); } if (thumbDef.width && thumbDef.height) { console.log(thumbDef); fileTransform.resize({ width: thumbDef.width, height: thumbDef.height }); } else if (!thumbDef.width && thumbDef.height) { fileTransform.resize({ height: thumbDef.height }); } else { fileTransform.resize(thumbDef.width); } await fileTransform.toFile(thumbFilePath); return bucket.upload(thumbFilePath, { destination: join(bucketDir, thumbFileName), metadata: metadata }); });
function* getClippedComponentScreenshot(req: express.Request, res: express.Response, next) { const state = yield select(); const { componentId, previewName } = req.params; const { uri } = (yield call(getComponentsScreenshotFromReq, req)) || { uri: null }; const { maxWidth, maxHeight } = req.query; if (!uri) { return next(); } const screenshot = getComponentScreenshot(componentId, previewName, state); if (!screenshot) { return next(); } const box = { left: screenshot.clip.left, top: screenshot.clip.top, width: screenshot.clip.right - screenshot.clip.left, height: screenshot.clip.bottom - screenshot.clip.top, }; let cw = box.width; let ch = box.height; let stream = sharp(uri).extract(box); if (maxWidth && cw > Number(maxWidth)) { const scale = Number(maxWidth) / cw; // 100 / 200 = 0.5 cw *= scale; ch *= scale; } if (maxHeight && ch > Number(maxHeight)) { const scale = Number(maxHeight) / ch; // 100 / 200 = 0.5 cw *= scale; ch *= scale; } if (cw !== box.width) { stream = stream.resize(Math.round(cw), Math.round(ch)); } const buffer = yield call(stream.toBuffer.bind(stream)); res.setHeader("Content-Length", buffer.length); res.setHeader("Content-Type", "image/png"); res.setHeader("Accept-Ranges", "bytes"); res.setHeader("Connection", "keep-alive"); res.end(buffer); // stream.pipe(res); }
async function processImage ( physicalFile: { path: string }, destination: string, newSize: { width: number, height: number } ) { await sharp(physicalFile.path) .resize(newSize.width, newSize.height) .toFile(destination) await unlinkPromise(physicalFile.path) }
registerBridge("fly.Image()", function imageConstructor( rt: Runtime, bridge: Bridge, data?: ivm.Reference<Buffer>, create?: any ) { try { if (data && !(data instanceof ArrayBuffer)) { throw new Error("image data must be an ArrayBuffer") } const opts: any = {} if (create) { if (typeof create.background === "string") { // create.background = color.parse(create.background) } opts.create = create } const image = sharp(data && Buffer.from(data), opts) const ref = new ivm.Reference(image) return Promise.resolve(ref) } catch (e) { return Promise.reject(e) } })
async function save(path: string, name: string, type: string, hash: string, size: number, metadata: any): Promise<IDriveFile> { let thumbnail: Buffer; if (['image/jpeg', 'image/png', 'image/webp'].includes(type)) { thumbnail = await sharp(path) .resize(300) .jpeg({ quality: 50, progressive: true }) .toBuffer(); } if (config.drive && config.drive.storage == 'minio') { const minio = new Minio.Client(config.drive.config); const key = `${config.drive.prefix}/${uuid.v4()}/${name}`; const thumbnailKey = `${config.drive.prefix}/${uuid.v4()}/${name}.thumbnail.jpg`; const baseUrl = config.drive.baseUrl || `${ config.drive.config.secure ? 'https' : 'http' }://${ config.drive.config.endPoint }${ config.drive.config.port ? ':' + config.drive.config.port : '' }/${ config.drive.bucket }`; await minio.putObject(config.drive.bucket, key, fs.createReadStream(path), size, { 'Content-Type': type, 'Cache-Control': 'max-age=31536000, immutable' }); if (thumbnail) { await minio.putObject(config.drive.bucket, thumbnailKey, thumbnail, size, { 'Content-Type': 'image/jpeg', 'Cache-Control': 'max-age=31536000, immutable' }); } Object.assign(metadata, { withoutChunks: true, storage: 'minio', storageProps: { key: key, thumbnailKey: thumbnailKey }, url: `${ baseUrl }/${ key }`, thumbnailUrl: thumbnail ? `${ baseUrl }/${ thumbnailKey }` : null }); const file = await DriveFile.insert({ length: size, uploadDate: new Date(), md5: hash, filename: name, metadata: metadata, contentType: type }); return file; } else { // Get MongoDB GridFS bucket const bucket = await getDriveFileBucket(); const file = await new Promise<IDriveFile>((resolve, reject) => { const writeStream = bucket.openUploadStream(name, { contentType: type, metadata }); writeStream.once('finish', resolve); writeStream.on('error', reject); fs.createReadStream(path).pipe(writeStream); }); if (thumbnail) { const thumbnailBucket = await getDriveFileThumbnailBucket(); await new Promise<IDriveFile>((resolve, reject) => { const writeStream = thumbnailBucket.openUploadStream(name, { contentType: 'image/jpeg', metadata: { originalId: file._id } }); writeStream.once('finish', resolve); writeStream.on('error', reject); writeStream.end(thumbnail); }); } return file; } }
/** * Add file to drive * * @param user User who wish to add file * @param path File path * @param name Name * @param comment Comment * @param folderId Folder ID * @param force If set to true, forcibly upload the file even if there is a file with the same hash. * @return Created drive file */ export default async function( user: IUser, path: string, name: string = null, comment: string = null, folderId: mongodb.ObjectID = null, force: boolean = false, isLink: boolean = false, url: string = null, uri: string = null, sensitive = false ): Promise<IDriveFile> { // Calc md5 hash const calcHash = new Promise<string>((res, rej) => { const readable = fs.createReadStream(path); const hash = crypto.createHash('md5'); const chunks: Buffer[] = []; readable .on('error', rej) .pipe(hash) .on('error', rej) .on('data', chunk => chunks.push(chunk)) .on('end', () => { const buffer = Buffer.concat(chunks); res(buffer.toString('hex')); }); }); // Detect content type const detectMime = new Promise<[string, string]>((res, rej) => { const readable = fs.createReadStream(path); readable .on('error', rej) .once('data', (buffer: Buffer) => { readable.destroy(); const type = fileType(buffer); if (type) { res([type.mime, type.ext]); } else { // 種類が同定できなかったら application/octet-stream にする res(['application/octet-stream', null]); } }); }); // Get file size const getFileSize = new Promise<number>((res, rej) => { fs.stat(path, (err, stats) => { if (err) return rej(err); res(stats.size); }); }); const [hash, [mime, ext], size] = await Promise.all([calcHash, detectMime, getFileSize]); log(`hash: ${hash}, mime: ${mime}, ext: ${ext}, size: ${size}`); // detect name const detectedName = name || (ext ? `untitled.${ext}` : 'untitled'); if (!force) { // Check if there is a file with the same hash const much = await DriveFile.findOne({ md5: hash, 'metadata.userId': user._id, 'metadata.deletedAt': { $exists: false } }); if (much) { log(`file with same hash is found: ${much._id}`); return much; } } //#region Check drive usage if (!isLink) { const usage = await DriveFile .aggregate([{ $match: { 'metadata.userId': user._id, 'metadata.deletedAt': { $exists: false } } }, { $project: { length: true } }, { $group: { _id: null, usage: { $sum: '$length' } } }]) .then((aggregates: any[]) => { if (aggregates.length > 0) { return aggregates[0].usage; } return 0; }); log(`drive usage is ${usage}`); const driveCapacity = 1024 * 1024 * (isLocalUser(user) ? config.localDriveCapacityMb : config.remoteDriveCapacityMb); // If usage limit exceeded if (usage + size > driveCapacity) { if (isLocalUser(user)) { throw 'no-free-space'; } else { // (アバターまたはバナーを含まず)最も古いファイルを削除する deleteOldFile(user); } } } //#endregion const fetchFolder = async () => { if (!folderId) { return null; } const driveFolder = await DriveFolder.findOne({ _id: folderId, userId: user._id }); if (driveFolder == null) throw 'folder-not-found'; return driveFolder; }; const properties: {[key: string]: any} = {}; let propPromises: Array<Promise<void>> = []; const isImage = ['image/jpeg', 'image/gif', 'image/png', 'image/webp'].includes(mime); if (isImage) { const img = sharp(path); // Calc width and height const calcWh = async () => { log('calculate image width and height...'); // Calculate width and height const meta = await img.metadata(); log(`image width and height is calculated: ${meta.width}, ${meta.height}`); properties['width'] = meta.width; properties['height'] = meta.height; }; // Calc average color const calcAvg = async () => { log('calculate average color...'); try { const info = await (img as any).stats(); const r = Math.round(info.channels[0].mean); const g = Math.round(info.channels[1].mean); const b = Math.round(info.channels[2].mean); log(`average color is calculated: ${r}, ${g}, ${b}`); const value = info.isOpaque ? [r, g, b] : [r, g, b, 255]; properties['avgColor'] = value; } catch (e) { } }; propPromises = [calcWh(), calcAvg()]; } const [folder] = await Promise.all([fetchFolder(), Promise.all(propPromises)]); const metadata = { userId: user._id, _user: { host: user.host }, folderId: folder !== null ? folder._id : null, comment: comment, properties: properties, withoutChunks: isLink, isRemote: isLink, isSensitive: sensitive } as IMetadata; if (url !== null) { metadata.src = url; if (isLink) { metadata.url = url; } } if (uri !== null) { metadata.uri = uri; } let driveFile: IDriveFile; if (isLink) { try { driveFile = await DriveFile.insert({ length: 0, uploadDate: new Date(), md5: hash, filename: detectedName, metadata: metadata, contentType: mime }); } catch (e) { // duplicate key error (when already registered) if (e.code === 11000) { log(`already registered ${metadata.uri}`); driveFile = await DriveFile.findOne({ 'metadata.uri': metadata.uri, 'metadata.userId': user._id }); } else { console.error(e); throw e; } } } else { driveFile = await (save(path, detectedName, mime, hash, size, metadata)); } log(`drive file has been created ${driveFile._id}`); pack(driveFile).then(packedFile => { // Publish drive_file_created event publishUserStream(user._id, 'drive_file_created', packedFile); publishDriveStream(user._id, 'file_created', packedFile); }); // TODO: サムネイル生成 return driveFile; }
import * as sharp from "sharp"; import { createReadStream, createWriteStream } from "fs"; // Test samples taken from the official documentation const input: Buffer = new Buffer(0); const readableStream: NodeJS.ReadableStream = createReadStream(input); const writableStream: NodeJS.WritableStream = createWriteStream(input); sharp(input) .extractChannel('green') .toFile('input_green.jpg', (err, info) => { // info.channels === 1 // input_green.jpg contains the green channel of the input image }); sharp('3-channel-rgb-input.png') .bandbool(sharp.bool.and) .toFile('1-channel-output.png', (err, info) => { // The output will be a single channel image where each pixel `P = R & G & B`. // If `I(1,1) = [247, 170, 14] = [0b11110111, 0b10101010, 0b00001111]` // then `O(1,1) = 0b11110111 & 0b10101010 & 0b00001111 = 0b00000010 = 2`. }); sharp('input.png') .rotate(180) .resize(300) .flatten() .background('#ff6600') .overlayWith('overlay.png', { gravity: sharp.gravity.southeast }) .sharpen()
new MongoObservable.Collection<Picture>('pictures') as PicturesCollection<Picture>; export const PicturesStore = new UploadFS.store.GridFS({ collection: Pictures.collection, name: 'pictures', filter: new UploadFS.Filter({ contentTypes: ['image/*'] }), permissions: new UploadFS.StorePermissions({ insert: picturesPermissions, update: picturesPermissions, remove: picturesPermissions }), transformWrite(from, to) { // Resize picture, then crop it to 1:1 aspect ratio, then compress it to 75% from its original quality const transform = sharp().resize(800,800).min().crop().toFormat('jpeg', {quality: 75}); from.pipe(transform).pipe(to); } }); // Gets picture's url by a given selector Pictures.getPictureUrl = function (selector, platform = "") { const prefix = platform === "android" ? "/android_asset/www" : platform === "ios" ? "" : ""; const picture = this.findOne(selector) || {}; return picture.url || prefix + DEFAULT_PICTURE_URL; }; function picturesPermissions(userId: string): boolean { return Meteor.isServer || !!userId;
async saveFile(blog: Blog, file: Express.Multer.File): Promise<string> { const extStart = file.originalname.lastIndexOf('.') const name = file.originalname.substr(0, extStart) // If the file is an image create thumbnails + convert to webp if (file.mimetype.includes('image')) { for (const height of IMAGE_SIZES) { await sharp(file.path) .resize(null, height) .webp({alphaQuality: 0}) .toFile(j(this.getDir(blog), name + '.h' + height + '.webp')) } // Convert to webp let img = sharp(file.path) .webp({alphaQuality: 0}) // if image is bigger than original size resize it if ((await img.stats()).channels.reduce((v, s) => (v > s.maxY) ? v : s.maxY, 0) > IMAGE_MAX_SIZE) { img = img.resize(null, IMAGE_MAX_SIZE) } await img.toFile(j(this.getDir(blog), name + '.webp')) // Delete original image await unlinker(file.path) return name + '.webp' } // Not an image, just move to right location await renamer(file.path, j(this.getDir(blog), file.originalname)) return file.originalname }