2020-02-23 17:34:40 -05:00
|
|
|
const { callbackify } = require('util')
|
2019-12-18 10:40:30 -05:00
|
|
|
const fs = require('fs')
|
2019-12-16 05:42:31 -05:00
|
|
|
const PersistorManager = require('./PersistorManager')
|
|
|
|
const LocalFileWriter = require('./LocalFileWriter')
|
|
|
|
const FileConverter = require('./FileConverter')
|
|
|
|
const KeyBuilder = require('./KeyBuilder')
|
|
|
|
const ImageOptimiser = require('./ImageOptimiser')
|
2020-01-07 16:19:26 -05:00
|
|
|
const { ConversionError } = require('./Errors')
|
2019-12-18 10:40:30 -05:00
|
|
|
|
|
|
|
module.exports = {
|
2020-02-23 17:34:40 -05:00
|
|
|
insertFile: callbackify(insertFile),
|
|
|
|
deleteFile: callbackify(deleteFile),
|
|
|
|
getFile: callbackify(getFile),
|
|
|
|
getFileSize: callbackify(getFileSize),
|
|
|
|
getDirectorySize: callbackify(getDirectorySize),
|
2019-12-18 10:40:30 -05:00
|
|
|
promises: {
|
2020-02-23 17:34:40 -05:00
|
|
|
getFile,
|
|
|
|
insertFile,
|
|
|
|
deleteFile,
|
|
|
|
getFileSize,
|
|
|
|
getDirectorySize
|
2019-12-18 10:40:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 17:34:40 -05:00
|
|
|
async function insertFile(bucket, key, stream) {
|
2019-12-18 10:40:30 -05:00
|
|
|
const convertedKey = KeyBuilder.getConvertedFolderKey(key)
|
2020-02-23 17:34:40 -05:00
|
|
|
await PersistorManager.promises.deleteDirectory(bucket, convertedKey)
|
|
|
|
await PersistorManager.promises.sendStream(bucket, key, stream)
|
2019-12-18 10:40:30 -05:00
|
|
|
}
|
|
|
|
|
2020-02-23 17:34:40 -05:00
|
|
|
async function deleteFile(bucket, key) {
|
2019-12-18 10:40:30 -05:00
|
|
|
const convertedKey = KeyBuilder.getConvertedFolderKey(key)
|
2020-02-23 17:34:40 -05:00
|
|
|
await Promise.all([
|
|
|
|
PersistorManager.promises.deleteFile(bucket, key),
|
|
|
|
PersistorManager.promises.deleteDirectory(bucket, convertedKey)
|
|
|
|
])
|
2019-12-18 10:40:30 -05:00
|
|
|
}
|
|
|
|
|
2020-02-23 17:34:40 -05:00
|
|
|
async function getFile(bucket, key, opts) {
|
2020-01-07 16:19:26 -05:00
|
|
|
opts = opts || {}
|
2019-12-18 10:40:30 -05:00
|
|
|
if (!opts.format && !opts.style) {
|
2020-02-23 17:34:40 -05:00
|
|
|
return PersistorManager.promises.getFileStream(bucket, key, opts)
|
2019-12-18 10:40:30 -05:00
|
|
|
} else {
|
2020-02-23 17:34:40 -05:00
|
|
|
return _getConvertedFile(bucket, key, opts)
|
2019-12-18 10:40:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 17:34:40 -05:00
|
|
|
async function getFileSize(bucket, key) {
|
|
|
|
return PersistorManager.promises.getFileSize(bucket, key)
|
2019-12-18 10:40:30 -05:00
|
|
|
}
|
|
|
|
|
2020-02-23 17:34:40 -05:00
|
|
|
async function getDirectorySize(bucket, projectId) {
|
|
|
|
return PersistorManager.promises.directorySize(bucket, projectId)
|
2019-12-18 10:40:30 -05:00
|
|
|
}
|
|
|
|
|
2020-02-23 17:34:40 -05:00
|
|
|
async function _getConvertedFile(bucket, key, opts) {
|
2019-12-18 10:40:30 -05:00
|
|
|
const convertedKey = KeyBuilder.addCachingToKey(key, opts)
|
2020-02-23 17:34:40 -05:00
|
|
|
const exists = await PersistorManager.promises.checkIfFileExists(
|
|
|
|
bucket,
|
|
|
|
convertedKey
|
|
|
|
)
|
|
|
|
if (exists) {
|
|
|
|
return PersistorManager.promises.getFileStream(bucket, convertedKey, opts)
|
|
|
|
} else {
|
|
|
|
return _getConvertedFileAndCache(bucket, key, convertedKey, opts)
|
|
|
|
}
|
2019-12-18 10:40:30 -05:00
|
|
|
}
|
|
|
|
|
2020-02-23 17:34:40 -05:00
|
|
|
async function _getConvertedFileAndCache(bucket, key, convertedKey, opts) {
|
2019-12-18 10:40:30 -05:00
|
|
|
let convertedFsPath
|
2020-02-23 17:34:40 -05:00
|
|
|
try {
|
|
|
|
convertedFsPath = await _convertFile(bucket, key, opts)
|
|
|
|
await ImageOptimiser.promises.compressPng(convertedFsPath)
|
|
|
|
await PersistorManager.promises.sendFile(
|
|
|
|
bucket,
|
|
|
|
convertedKey,
|
|
|
|
convertedFsPath
|
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
LocalFileWriter.deleteFile(convertedFsPath, () => {})
|
|
|
|
throw new ConversionError({
|
|
|
|
message: 'failed to convert file',
|
|
|
|
info: { opts, bucket, key, convertedKey }
|
|
|
|
}).withCause(err)
|
|
|
|
}
|
|
|
|
// Send back the converted file from the local copy to avoid problems
|
|
|
|
// with the file not being present in S3 yet. As described in the
|
|
|
|
// documentation below, we have already made a 'HEAD' request in
|
|
|
|
// checkIfFileExists so we only have "eventual consistency" if we try
|
|
|
|
// to stream it from S3 here. This was a cause of many 403 errors.
|
|
|
|
//
|
|
|
|
// "Amazon S3 provides read-after-write consistency for PUTS of new
|
|
|
|
// objects in your S3 bucket in all regions with one caveat. The
|
|
|
|
// caveat is that if you make a HEAD or GET request to the key name
|
|
|
|
// (to find if the object exists) before creating the object, Amazon
|
|
|
|
// S3 provides eventual consistency for read-after-write.""
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/dev/Introduction.html#ConsistencyModel
|
|
|
|
const readStream = fs.createReadStream(convertedFsPath)
|
|
|
|
readStream.on('end', function() {
|
|
|
|
LocalFileWriter.deleteFile(convertedFsPath, function() {})
|
|
|
|
})
|
|
|
|
return readStream
|
2019-12-18 10:40:30 -05:00
|
|
|
}
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2020-02-23 17:34:40 -05:00
|
|
|
async function _convertFile(bucket, originalKey, opts) {
|
|
|
|
let originalFsPath
|
|
|
|
try {
|
|
|
|
originalFsPath = await _writeFileToDisk(bucket, originalKey, opts)
|
|
|
|
} catch (err) {
|
|
|
|
throw new ConversionError({
|
|
|
|
message: 'unable to write file to disk',
|
|
|
|
info: { bucket, originalKey, opts }
|
|
|
|
}).withCause(err)
|
|
|
|
}
|
2014-03-04 08:36:47 -05:00
|
|
|
|
2020-02-23 17:34:40 -05:00
|
|
|
let promise
|
|
|
|
if (opts.format) {
|
|
|
|
promise = FileConverter.promises.convert(originalFsPath, opts.format)
|
|
|
|
} else if (opts.style === 'thumbnail') {
|
|
|
|
promise = FileConverter.promises.thumbnail(originalFsPath)
|
|
|
|
} else if (opts.style === 'preview') {
|
|
|
|
promise = FileConverter.promises.preview(originalFsPath)
|
|
|
|
} else {
|
|
|
|
throw new ConversionError({
|
|
|
|
message: 'invalid file conversion options',
|
|
|
|
info: {
|
|
|
|
bucket,
|
|
|
|
originalKey,
|
|
|
|
opts
|
2019-12-16 05:42:31 -05:00
|
|
|
}
|
2020-02-23 17:34:40 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
let destPath
|
|
|
|
try {
|
|
|
|
destPath = await promise
|
|
|
|
} catch (err) {
|
|
|
|
throw new ConversionError({
|
|
|
|
message: 'error converting file',
|
|
|
|
info: { bucket, originalKey, opts }
|
|
|
|
}).withCause(err)
|
|
|
|
}
|
|
|
|
LocalFileWriter.deleteFile(originalFsPath, function() {})
|
|
|
|
return destPath
|
2019-12-18 10:40:30 -05:00
|
|
|
}
|
|
|
|
|
2020-02-23 17:34:40 -05:00
|
|
|
async function _writeFileToDisk(bucket, key, opts) {
|
|
|
|
const fileStream = await PersistorManager.promises.getFileStream(
|
|
|
|
bucket,
|
|
|
|
key,
|
|
|
|
opts
|
|
|
|
)
|
|
|
|
return LocalFileWriter.promises.writeStream(fileStream, key)
|
2019-12-18 10:40:30 -05:00
|
|
|
}
|