return processed file sizes

This commit is contained in:
Martin McKeaveney 2020-09-18 11:01:39 +01:00
parent 9be2b7d691
commit af9b8ac9bd
3 changed files with 20 additions and 11 deletions

View File

@ -89,7 +89,11 @@
class={`file-icon ${determineFileIcon(selectedImage.extension)}`} />
<span class="filename">{selectedImage.name}</span>
</div>
<p>{selectedImage.size / 1000}KB</p>
<p>
{#if selectedImage.size <= 1000000}
{selectedImage.size / 1000}KB
{:else}{selectedImage.size / 1000000}MB{/if}
</p>
</header>
<div class="delete-button" on:click={removeFile}>
<i class="ri-close-line" />

View File

@ -70,8 +70,7 @@ exports.processLocalFileUpload = async function(ctx) {
)
try {
// TODO: get file sizes of images after resize
await Promise.all(fileProcessOperations)
const processedFiles = await Promise.all(fileProcessOperations)
let pendingFileUploads
// local document used to track which files need to be uploaded
@ -88,12 +87,12 @@ exports.processLocalFileUpload = async function(ctx) {
})
pendingFileUploads.uploads = [
...filesToProcess,
...processedFiles,
...pendingFileUploads.uploads,
]
await db.put(pendingFileUploads)
ctx.body = filesToProcess
ctx.body = processedFiles
} catch (err) {
ctx.throw(500, err)
}

View File

@ -6,19 +6,25 @@ const FORMATS = {
IMAGES: ["png", "jpg", "jpeg", "gif", "svg", "tiff", "raw"],
}
function processImage({ path, outputPath }) {
return sharp(path)
async function processImage(file) {
const imgMeta = await sharp(file.path)
.resize(300)
.toFile(outputPath)
.toFile(file.outputPath)
return {
...file,
...imgMeta,
}
}
function process(file) {
async function process(file) {
if (FORMATS.IMAGES.includes(file.extension.toLowerCase())) {
return processImage(file)
return await processImage(file)
}
// No processing required
return fsPromises.copyFile(file.path, file.outputPath)
await fsPromises.copyFile(file.path, file.outputPath)
return file
}
exports.process = process