Fix thumbnail processing

pull/1462/head
Chocobozzz 2018-12-04 15:12:54 +01:00
parent 2beb98952a
commit 745778256c
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
6 changed files with 25 additions and 20 deletions

View File

@ -1,6 +1,7 @@
import 'multer' import 'multer'
import * as sharp from 'sharp' import * as sharp from 'sharp'
import { move, remove } from 'fs-extra' import { readFile, remove } from 'fs-extra'
import { logger } from './logger'
async function processImage ( async function processImage (
physicalFile: { path: string }, physicalFile: { path: string },
@ -11,14 +12,11 @@ async function processImage (
throw new Error('Sharp needs an input path different that the output path.') throw new Error('Sharp needs an input path different that the output path.')
} }
const sharpInstance = sharp(physicalFile.path) logger.debug('Processing image %s to %s.', physicalFile.path, destination)
const metadata = await sharpInstance.metadata()
// No need to resize // Avoid sharp cache
if (metadata.width === newSize.width && metadata.height === newSize.height) { const buf = await readFile(physicalFile.path)
await move(physicalFile.path, destination, { overwrite: true }) const sharpInstance = sharp(buf)
return
}
await remove(destination) await remove(destination)

View File

@ -3,6 +3,7 @@ import { createWriteStream } from 'fs-extra'
import * as request from 'request' import * as request from 'request'
import { ACTIVITY_PUB } from '../initializers' import { ACTIVITY_PUB } from '../initializers'
import { processImage } from './image-utils' import { processImage } from './image-utils'
import { extname } from 'path'
function doRequest <T> ( function doRequest <T> (
requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean } requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean }
@ -29,8 +30,7 @@ function doRequestAndSaveToFile (requestOptions: request.CoreOptions & request.U
} }
async function downloadImage (url: string, destPath: string, size: { width: number, height: number }) { async function downloadImage (url: string, destPath: string, size: { width: number, height: number }) {
const tmpPath = destPath + '.tmp' const tmpPath = destPath + '.tmp' + extname(destPath)
await doRequestAndSaveToFile({ method: 'GET', uri: url }, tmpPath) await doRequestAndSaveToFile({ method: 'GET', uri: url }, tmpPath)
await processImage({ path: tmpPath }, destPath, size) await processImage({ path: tmpPath }, destPath, size)

View File

@ -51,7 +51,7 @@ async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate)
return undefined return undefined
} }
const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id }) const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false })
const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject) const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
const updateOptions = { const updateOptions = {

View File

@ -158,25 +158,30 @@ async function syncVideoExternalAttributes (video: VideoModel, fetchedVideo: Vid
async function getOrCreateVideoAndAccountAndChannel (options: { async function getOrCreateVideoAndAccountAndChannel (options: {
videoObject: VideoTorrentObject | string, videoObject: VideoTorrentObject | string,
syncParam?: SyncParam, syncParam?: SyncParam,
fetchType?: VideoFetchByUrlType fetchType?: VideoFetchByUrlType,
allowRefresh?: boolean // true by default
}) { }) {
// Default params // Default params
const syncParam = options.syncParam || { likes: true, dislikes: true, shares: true, comments: true, thumbnail: true, refreshVideo: false } const syncParam = options.syncParam || { likes: true, dislikes: true, shares: true, comments: true, thumbnail: true, refreshVideo: false }
const fetchType = options.fetchType || 'all' const fetchType = options.fetchType || 'all'
const allowRefresh = options.allowRefresh !== false
// Get video url // Get video url
const videoUrl = getAPUrl(options.videoObject) const videoUrl = getAPUrl(options.videoObject)
let videoFromDatabase = await fetchVideoByUrl(videoUrl, fetchType) let videoFromDatabase = await fetchVideoByUrl(videoUrl, fetchType)
if (videoFromDatabase) { if (videoFromDatabase) {
const refreshOptions = {
video: videoFromDatabase,
fetchedType: fetchType,
syncParam
}
if (syncParam.refreshVideo === true) videoFromDatabase = await refreshVideoIfNeeded(refreshOptions) if (allowRefresh === true) {
else await JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', videoUrl: videoFromDatabase.url } }) const refreshOptions = {
video: videoFromDatabase,
fetchedType: fetchType,
syncParam
}
if (syncParam.refreshVideo === true) videoFromDatabase = await refreshVideoIfNeeded(refreshOptions)
else await JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', videoUrl: videoFromDatabase.url } })
}
return { video: videoFromDatabase } return { video: videoFromDatabase }
} }

View File

@ -10,7 +10,8 @@ export type RefreshPayload = {
async function refreshAPObject (job: Bull.Job) { async function refreshAPObject (job: Bull.Job) {
const payload = job.data as RefreshPayload const payload = job.data as RefreshPayload
logger.info('Processing AP refresher in job %d.', job.id)
logger.info('Processing AP refresher in job %d for video %s.', job.id, payload.videoUrl)
if (payload.type === 'video') return refreshAPVideo(payload.videoUrl) if (payload.type === 'video') return refreshAPVideo(payload.videoUrl)
} }

View File

@ -1,6 +1,7 @@
// Order of the tests we want to execute // Order of the tests we want to execute
import './create-import-video-file-job' import './create-import-video-file-job'
import './create-transcoding-job' import './create-transcoding-job'
import './optimize-old-videos'
import './peertube' import './peertube'
import './reset-password' import './reset-password'
import './update-host' import './update-host'