Newer
Older
/* eslint-disable import/no-unresolved */
const logger = require('@pubsweet/logger')
const mime = require('mime-types')
const fs = require('fs-extra')
const path = require('path')
const sharp = require('sharp')
const { useTransaction } = require('@coko/server')
const File = require('@coko/server/src/models/file/file.model')
const {
connectToFileStorage,
download,
} = require('@coko/server/src/services/fileStorage')
const {
convertFileStreamIntoBuffer,
getFileExtension,
getImageFileMetadata,
} = require('@coko/server/src/helpers')
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const imageSizeConversionMapper = {
tiff: {
full: 'png',
},
tif: {
full: 'png',
},
svg: {
full: 'svg',
},
png: {
full: 'png',
},
default: {
full: 'jpeg',
},
}
const sharpConversionFullFilePath = async (
buffer,
tempDir,
filenameWithoutExtension,
format,
) => {
await fs.ensureDir(tempDir)
const tempFullFilePath = path.join(
tempDir,
`${filenameWithoutExtension}_full.${
imageSizeConversionMapper[format]
? imageSizeConversionMapper[format].full
: imageSizeConversionMapper.default.full
}`,
)
await sharp(buffer).toFile(tempFullFilePath)
return tempFullFilePath
}
exports.up = async knex => {
try {
return useTransaction(async trx => {
await connectToFileStorage()
const files = await File.query(trx)
const tempDir = path.join(__dirname, '..', 'temp')
await fs.ensureDir(tempDir)
await Promise.all(
files.map(async file => {
const mimetype = mime.lookup(file.name)
if (mimetype.match(/^image\//)) {
const originalStoredObject = file.storedObjects.find(
storedObject => storedObject.type === 'original',
)
const filenameWithoutExtension = path.parse(
originalStoredObject.key,
).name
const tempPath = path.join(tempDir, originalStoredObject.key)
await download(originalStoredObject.key, tempPath)
const format = originalStoredObject.extension
const buffer = fs.readFileSync(tempPath)
const tempFullFilePath = await sharpConversionFullFilePath(
buffer,
tempDir,
filenameWithoutExtension,
format,
)
fs.unlinkSync(tempPath)
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const fullImageStream = fs.createReadStream(tempFullFilePath)
const full = await uploadFileHandler(
fs.createReadStream(tempFullFilePath),
path.basename(tempFullFilePath),
mime.lookup(tempFullFilePath),
)
const fullFileBuffer = await convertFileStreamIntoBuffer(
fullImageStream,
)
const {
width: fWidth,
height: fHeight,
space: fSpace,
density: fDensity,
size: fSize,
} = await getImageFileMetadata(fullFileBuffer)
full.imageMetadata = {
density: fDensity,
height: fHeight,
space: fSpace,
width: fWidth,
}
full.size = fSize
full.extension = `${getFileExtension(tempFullFilePath)}`
full.type = 'full'
full.mimetype = mime.lookup(tempFullFilePath)
file.storedObjects.push(full)
await File.query(trx).patchAndFetchById(file.id, {
storedObjects: file.storedObjects,
})
fs.unlinkSync(tempFullFilePath)
}
}),
)
return true
})
} catch (e) {
logger.error(
'File: Add full conversion image quality to stored objects migration failed!',
)
throw new Error(e)
}
}