Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
152
153
154
155
156
157
158
159
160
161
162
163
/* eslint-disable import/extensions */
/* eslint-disable import/no-unresolved */
const logger = require('@pubsweet/logger')
const mime = require('mime-types')
const axios = require('axios')
const fs = require('fs-extra')
const path = require('path')
const sharp = require('sharp')
const useTransaction = require('../src/models/useTransaction')
const File = require('../src/models/file/file.model')
const {
connectToFileStorage,
getURL,
uploadFileHandler,
} = require('../src/services/fileStorage')
const {
convertFileStreamIntoBuffer,
getFileExtension,
getImageFileMetadata,
} = require('../src/helpers')
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
}
const createMinioFileImageStream = async url => {
const response = await axios.get(url, {
responseType: 'stream',
})
return response.data
}
exports.up = async knex => {
try {
return useTransaction(async trx => {
await connectToFileStorage()
const files = await File.query(trx)
const tempDir = path.join(__dirname, '..', 'temp')
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 fileURL = await getURL(originalStoredObject.key)
const filenameWithoutExtension = path.parse(
originalStoredObject.key,
).name
const minioFileImageStream = await createMinioFileImageStream(
fileURL,
)
const format = originalStoredObject.extension
const buffer = await convertFileStreamIntoBuffer(
minioFileImageStream,
)
const tempFullFilePath = await sharpConversionFullFilePath(
buffer,
tempDir,
filenameWithoutExtension,
format,
)
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)
}
}