import 'multer'
import * as sharp from 'sharp'
-import { remove } from 'fs-extra'
+import { move, remove } from 'fs-extra'
async function processImage (
physicalFile: { path: string },
destination: string,
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)
.toFile(destination)
if (options.updateViews === true) options.video.set('views', videoData.views)
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 newVideoFiles = videoFileAttributes.map(a => new VideoFileModel(a))
logger.debug('Cannot update the remote video.', { 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 {