mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
skip uploading if file already exists
This commit is contained in:
parent
6f7c58289a
commit
6bb81dd5c3
2 changed files with 20 additions and 5 deletions
|
|
@ -6,7 +6,7 @@ import { getClaimsByToken, getTokenByRequest } from '../utils/auth'
|
|||
import { corsConfig } from '../utils/corsConfig'
|
||||
import { enqueueBulkUploadContentJob } from '../utils/createTask'
|
||||
import { logger } from '../utils/logger'
|
||||
import { generateDownloadSignedUrl } from '../utils/uploads'
|
||||
import { generateDownloadSignedUrl, isFileExists } from '../utils/uploads'
|
||||
|
||||
export function contentRouter() {
|
||||
const router = Router()
|
||||
|
|
@ -77,12 +77,19 @@ export function contentRouter() {
|
|||
expires: Date.now() + 60 * 60 * 1000, // 1 hour
|
||||
})
|
||||
|
||||
// check if file is already uploaded
|
||||
const exists = await isFileExists(filePath)
|
||||
if (exists) {
|
||||
logger.info('File already exists', filePath)
|
||||
}
|
||||
|
||||
return {
|
||||
libraryItemId: libraryItem.id,
|
||||
userId,
|
||||
filePath,
|
||||
downloadUrl,
|
||||
format,
|
||||
exists,
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Error while generating signed url', error)
|
||||
|
|
@ -95,12 +102,15 @@ export function contentRouter() {
|
|||
)
|
||||
logger.info('Signed urls generated', data)
|
||||
|
||||
const validData = data.filter(
|
||||
(d) => d.downloadUrl !== undefined && !('error' in d)
|
||||
// skip uploading if there is an error or file already exists
|
||||
const uploadData = data.filter(
|
||||
(d) => !('error' in d) && d.downloadUrl !== undefined && !d.exists
|
||||
) as UploadContentJobData[]
|
||||
|
||||
await enqueueBulkUploadContentJob(validData)
|
||||
logger.info('Bulk upload content job enqueued', validData)
|
||||
if (uploadData.length > 0) {
|
||||
await enqueueBulkUploadContentJob(uploadData)
|
||||
logger.info('Bulk upload content job enqueued', uploadData)
|
||||
}
|
||||
|
||||
res.send({
|
||||
data: data.map((d) => ({
|
||||
|
|
|
|||
|
|
@ -148,3 +148,8 @@ export const uploadToSignedUrl = async (
|
|||
timeout,
|
||||
})
|
||||
}
|
||||
|
||||
export const isFileExists = async (filePath: string): Promise<boolean> => {
|
||||
const [exists] = await storage.bucket(bucketName).file(filePath).exists()
|
||||
return exists
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue