1
0
Fork 0

Optimize image resizing

This commit is contained in:
Chocobozzz 2018-11-19 11:24:31 +01:00
parent 9d0b856e93
commit a8a6322778
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 23 additions and 7 deletions

View File

@ -25,7 +25,6 @@ export class VerifyAccountEmailComponent implements OnInit {
} }
ngOnInit () { ngOnInit () {
this.userId = this.route.snapshot.queryParams['userId'] this.userId = this.route.snapshot.queryParams['userId']
this.verificationString = this.route.snapshot.queryParams['verificationString'] this.verificationString = this.route.snapshot.queryParams['verificationString']

View File

@ -1,13 +1,28 @@
import 'multer' import 'multer'
import * as sharp from 'sharp' import * as sharp from 'sharp'
import { remove } from 'fs-extra' import { move, remove } from 'fs-extra'
async function processImage ( async function processImage (
physicalFile: { path: string }, physicalFile: { path: string },
destination: string, destination: string,
newSize: { width: number, height: number } newSize: { width: number, height: number }
) { ) {
await sharp(physicalFile.path) if (physicalFile.path === destination) {
throw new Error('Sharp needs an input path different that the output path.')
}
const sharpInstance = sharp(physicalFile.path)
const metadata = await sharpInstance.metadata()
// No need to resize
if (metadata.width === newSize.width && metadata.height === newSize.height) {
await move(physicalFile.path, destination, { overwrite: true })
return
}
await remove(destination)
await sharpInstance
.resize(newSize.width, newSize.height) .resize(newSize.width, newSize.height)
.toFile(destination) .toFile(destination)

View File

@ -242,10 +242,6 @@ async function updateVideoFromAP (options: {
if (options.updateViews === true) options.video.set('views', videoData.views) if (options.updateViews === true) options.video.set('views', videoData.views)
await options.video.save(sequelizeOptions) await options.video.save(sequelizeOptions)
// Don't block on request
generateThumbnailFromUrl(options.video, options.videoObject.icon)
.catch(err => logger.warn('Cannot generate thumbnail of %s.', options.videoObject.id, { err }))
{ {
const videoFileAttributes = videoFileActivityUrlToDBAttributes(options.video, options.videoObject) const videoFileAttributes = videoFileActivityUrlToDBAttributes(options.video, options.videoObject)
const newVideoFiles = videoFileAttributes.map(a => new VideoFileModel(a)) const newVideoFiles = videoFileAttributes.map(a => new VideoFileModel(a))
@ -293,6 +289,12 @@ async function updateVideoFromAP (options: {
logger.debug('Cannot update the remote video.', { err }) logger.debug('Cannot update the remote video.', { err })
throw err throw err
} }
try {
await generateThumbnailFromUrl(options.video, options.videoObject.icon)
} catch (err) {
logger.warn('Cannot generate thumbnail of %s.', options.videoObject.id, { err })
}
} }
export { export {