aboutsummaryrefslogtreecommitdiff
path: root/src/server/DashUploadUtils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/DashUploadUtils.ts')
-rw-r--r--src/server/DashUploadUtils.ts77
1 files changed, 29 insertions, 48 deletions
diff --git a/src/server/DashUploadUtils.ts b/src/server/DashUploadUtils.ts
index 19cb3f240..2053ae448 100644
--- a/src/server/DashUploadUtils.ts
+++ b/src/server/DashUploadUtils.ts
@@ -5,8 +5,7 @@ import { File } from 'formidable';
import { createReadStream, createWriteStream, existsSync, readFileSync, rename, unlinkSync, writeFile } from 'fs';
import * as path from 'path';
import { basename } from 'path';
-import * as sharp from 'sharp';
-import { Readable, Stream } from 'stream';
+import Jimp from 'jimp';
import { filesDirectory, publicDirectory } from '.';
import { Opt } from '../fields/Doc';
import { ParsedPDF } from '../server/PdfTypes';
@@ -55,9 +54,9 @@ export namespace DashUploadUtils {
}
export const Sizes: { [size: string]: Size } = {
- SMALL: { width: 100, suffix: SizeSuffix.Small },
+ LARGE: { width: 800, suffix: SizeSuffix.Large },
MEDIUM: { width: 400, suffix: SizeSuffix.Medium },
- LARGE: { width: 900, suffix: SizeSuffix.Large },
+ SMALL: { width: 100, suffix: SizeSuffix.Small },
};
export function validateExtension(url: string) {
@@ -197,7 +196,7 @@ export namespace DashUploadUtils {
const isAzureOn = usingAzure();
const { type, path, name } = file;
const types = type?.split('/') ?? [];
- uploadProgress.set(overwriteGuid ?? name, 'uploading'); // If the client sent a guid it uses to track upload progress, use that guid. Otherwise, use the file's name.
+ // uploadProgress.set(overwriteGuid ?? name, 'uploading'); // If the client sent a guid it uses to track upload progress, use that guid. Otherwise, use the file's name.
const category = types[0];
let format = `.${types[1]}`;
@@ -367,7 +366,7 @@ export namespace DashUploadUtils {
}
export interface ImageResizer {
- resizer?: sharp.Sharp;
+ width?: any; // sharp.Sharp;
suffix: SizeSuffix;
}
@@ -540,7 +539,7 @@ export namespace DashUploadUtils {
writtenFiles = {};
}
} else {
- writtenFiles = await outputResizedImages(() => request(requestable), resolved, pathToDirectory(Directory.images));
+ writtenFiles = await outputResizedImages(metadata.source, resolved, pathToDirectory(Directory.images));
}
for (const suffix of Object.keys(writtenFiles)) {
information.accessPaths[suffix] = getAccessPaths(images, writtenFiles[suffix]);
@@ -595,57 +594,39 @@ export namespace DashUploadUtils {
* @param outputDirectory the directory to output to, usually Directory.Images
* @returns a map with suffixes as keys and resized filenames as values.
*/
- export async function outputResizedImages(streamProvider: () => Stream | Promise<Stream>, outputFileName: string, outputDirectory: string) {
+ export async function outputResizedImages(sourcePath: string, outputFileName: string, outputDirectory: string) {
const writtenFiles: { [suffix: string]: string } = {};
- for (const { resizer, suffix } of resizers(path.extname(outputFileName))) {
- const outputPath = path.resolve(outputDirectory, (writtenFiles[suffix] = InjectSize(outputFileName, suffix)));
- await new Promise<void>(async (resolve, reject) => {
- const source = streamProvider();
- let readStream = source instanceof Promise ? await source : source;
- let error = false;
- if (resizer) {
- readStream = readStream.pipe(resizer.withMetadata()).on('error', async args => {
- error = true;
- if (error) {
- const source2 = streamProvider();
- let readStream2: Stream | undefined;
- readStream2 = source2 instanceof Promise ? await source2 : source2;
- readStream2?.pipe(createWriteStream(outputPath)).on('error', resolve).on('close', resolve);
- }
- });
- }
- !error && readStream?.pipe(createWriteStream(outputPath)).on('error', resolve).on('close', resolve);
+ const sizes = imageResampleSizes(path.extname(outputFileName));
+ const outputPath = (suffix: SizeSuffix) => path.resolve(outputDirectory, (writtenFiles[suffix] = InjectSize(outputFileName, suffix)));
+ await Promise.all(
+ sizes.filter(({ width }) => !width).map(({ suffix }) =>
+ new Promise<void>(res => createReadStream(sourcePath).pipe(createWriteStream(outputPath(suffix))).on('close', res))
+ )); // prettier-ignore
+ return Jimp.read(sourcePath)
+ .then(async img => {
+ await Promise.all( sizes.filter(({ width }) => width) .map(({ width, suffix }) =>
+ img = img.resize(width, Jimp.AUTO).write(outputPath(suffix))
+ )); // prettier-ignore
+ return writtenFiles;
+ })
+ .catch(e => {
+ console.log('ERROR' + e);
+ return writtenFiles;
});
- }
- return writtenFiles;
}
/**
* define the resizers to use
* @param ext the extension
- * @returns an array of resizer functions from sharp
+ * @returns an array of resize descriptions
*/
- function resizers(ext: string): DashUploadUtils.ImageResizer[] {
+ export function imageResampleSizes(ext: string): DashUploadUtils.ImageResizer[] {
return [
{ suffix: SizeSuffix.Original },
- ...Object.values(DashUploadUtils.Sizes).map(({ suffix, width }) => {
- let initial: sharp.Sharp | undefined = sharp({ failOnError: false }).resize(width, undefined, { withoutEnlargement: true });
- if (pngs.includes(ext)) {
- initial = initial.png(pngOptions);
- } else if (jpgs.includes(ext)) {
- initial = initial.jpeg();
- } else if (webps.includes(ext)) {
- initial = initial.webp();
- } else if (tiffs.includes(ext)) {
- initial = initial.tiff();
- } else if (ext === '.gif') {
- initial = undefined;
- }
- return {
- resizer: suffix === '_o' ? undefined : initial,
- suffix,
- };
- }),
+ ...[...(AcceptableMedia.imageFormats.includes(ext.toLowerCase()) ? Object.values(DashUploadUtils.Sizes) : [])].map(({ suffix, width }) => ({
+ width,
+ suffix,
+ })),
];
}
}