Skip to content
Snippets Groups Projects
Commit 22f02180 authored by Vignesh Devendran's avatar Vignesh Devendran :dart: Committed by Yannis Barlas
Browse files

fix(models): fix migration failing when multiple files use the same object key

parent c4a387a0
No related branches found
No related tags found
No related merge requests found
......@@ -40,14 +40,14 @@ const imageSizeConversionMapper = {
const sharpConversionFullFilePath = async (
buffer,
tempDir,
tempFileDir,
filenameWithoutExtension,
format,
) => {
await fs.ensureDir(tempDir)
await fs.ensureDir(tempFileDir)
const tempFullFilePath = path.join(
tempDir,
tempFileDir,
`${filenameWithoutExtension}_full.${
imageSizeConversionMapper[format]
? imageSizeConversionMapper[format].full
......@@ -73,7 +73,14 @@ exports.up = async knex => {
files.map(async file => {
const mimetype = mime.lookup(file.name)
if (mimetype.match(/^image\//)) {
const fullStoredObject = file.storedObjects.find(
storedObject => storedObject.type === 'full',
)
if (mimetype.match(/^image\//) && !fullStoredObject) {
const tempFileDir = path.join(__dirname, '..', 'temp', file.id)
await fs.ensureDir(tempFileDir)
const originalStoredObject = file.storedObjects.find(
storedObject => storedObject.type === 'original',
)
......@@ -82,7 +89,8 @@ exports.up = async knex => {
originalStoredObject.key,
).name
const tempPath = path.join(tempDir, originalStoredObject.key)
const tempPath = path.join(tempFileDir, originalStoredObject.key)
await download(originalStoredObject.key, tempPath)
const format = originalStoredObject.extension
......@@ -91,7 +99,7 @@ exports.up = async knex => {
const tempFullFilePath = await sharpConversionFullFilePath(
buffer,
tempDir,
tempFileDir,
filenameWithoutExtension,
format,
)
......@@ -140,6 +148,13 @@ exports.up = async knex => {
}),
)
try {
await fs.remove(tempDir)
} catch (e) {
logger.error(e)
throw new Error(e)
}
return true
})
} catch (e) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment