aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Wilkins <samwilkins333@gmail.com>2020-01-24 11:56:37 -0500
committerSam Wilkins <samwilkins333@gmail.com>2020-01-24 11:56:37 -0500
commit416541c18545cabe0c1b25d698770d7a50a9136e (patch)
treec6b5a7cc1057799751b3acb0245094909cce161d /src
parentf984fab919c5c93937778103bbda47a058c57df2 (diff)
finalized image upload changes, fixed exif parsing, excessive image uploading and automatically encode native dimensions
Diffstat (limited to 'src')
-rw-r--r--src/client/util/Import & Export/ImageUtils.ts12
-rw-r--r--src/client/util/request-image-size.js2
-rw-r--r--src/client/views/collections/CollectionSubView.tsx13
-rw-r--r--src/client/views/nodes/ImageBox.tsx42
-rw-r--r--src/server/ApiManagers/UploadManager.ts3
-rw-r--r--src/server/DashUploadUtils.ts56
6 files changed, 69 insertions, 59 deletions
diff --git a/src/client/util/Import & Export/ImageUtils.ts b/src/client/util/Import & Export/ImageUtils.ts
index 6a9486f83..2f4db0e17 100644
--- a/src/client/util/Import & Export/ImageUtils.ts
+++ b/src/client/util/Import & Export/ImageUtils.ts
@@ -14,9 +14,17 @@ export namespace ImageUtils {
return false;
}
const source = field.url.href;
- const response = await Networking.PostToServer("/inspectImage", { source });
- const { error, data } = response.exifData;
+ const {
+ contentSize,
+ nativeWidth,
+ nativeHeight,
+ exifData: { error, data }
+ } = await Networking.PostToServer("/inspectImage", { source });
document.exif = error || Docs.Get.DocumentHierarchyFromJson(data);
+ const proto = Doc.GetProto(document);
+ proto.nativeWidth = nativeWidth;
+ proto.nativeHeight = nativeHeight;
+ proto.contentSize = contentSize;
return data !== undefined;
};
diff --git a/src/client/util/request-image-size.js b/src/client/util/request-image-size.js
index 27605d167..beb030635 100644
--- a/src/client/util/request-image-size.js
+++ b/src/client/util/request-image-size.js
@@ -38,7 +38,7 @@ module.exports = function requestImageSize(options) {
return reject(new HttpError(res.statusCode, res.statusMessage));
}
- let buffer = new Buffer([]);
+ let buffer = new Buffer.from([]);
let size;
let imageSizeError;
diff --git a/src/client/views/collections/CollectionSubView.tsx b/src/client/views/collections/CollectionSubView.tsx
index 063b5c5df..251828bbd 100644
--- a/src/client/views/collections/CollectionSubView.tsx
+++ b/src/client/views/collections/CollectionSubView.tsx
@@ -302,12 +302,19 @@ export function CollectionSubView<T>(schemaCtor: (doc: Doc) => T) {
formData.append('file', file);
const dropFileName = file ? file.name : "-empty-";
promises.push(Networking.PostFormDataToServer("/upload", formData).then(results => {
- results.map(action(({ clientAccessPath }: any) => {
+ results.map(action((result: any) => {
+ const { clientAccessPath, nativeWidth, nativeHeight, contentSize } = result;
const full = { ...options, width: 300, title: dropFileName };
const pathname = Utils.prepend(clientAccessPath);
Docs.Get.DocumentFromType(type, pathname, full).then(doc => {
- doc && (Doc.GetProto(doc).fileUpload = basename(pathname).replace("upload_", "").replace(/\.[a-z0-9]*$/, ""));
- doc && this.props.addDocument(doc);
+ if (doc) {
+ const proto = Doc.GetProto(doc);
+ proto.fileUpload = basename(pathname).replace("upload_", "").replace(/\.[a-z0-9]*$/, "");
+ nativeWidth && (proto.nativeWidth = nativeWidth);
+ nativeHeight && (proto.nativeHeight = nativeHeight);
+ contentSize && (proto.contentSize = contentSize);
+ this.props.addDocument(doc);
+ }
});
}));
}));
diff --git a/src/client/views/nodes/ImageBox.tsx b/src/client/views/nodes/ImageBox.tsx
index b7e904283..c47e656fe 100644
--- a/src/client/views/nodes/ImageBox.tsx
+++ b/src/client/views/nodes/ImageBox.tsx
@@ -214,26 +214,26 @@ export class ImageBox extends DocAnnotatableComponent<FieldViewProps, ImageDocum
}
_curSuffix = "_m";
- _resized = "";
- resize = (imgPath: string) => {
- requestImageSize(imgPath)
- .then((size: any) => {
- const rotation = NumCast(this.dataDoc.rotation) % 180;
- const realsize = rotation === 90 || rotation === 270 ? { height: size.width, width: size.height } : size;
- const aspect = realsize.height / realsize.width;
- if (this.Document.width && (Math.abs(1 - NumCast(this.Document.height) / NumCast(this.Document.width) / (realsize.height / realsize.width)) > 0.1)) {
- setTimeout(action(() => {
- if (this.paths[NumCast(this.props.Document.curPage)] === imgPath && (!this.layoutDoc.isTemplateDoc || this.dataDoc !== this.layoutDoc)) {
- this._resized = imgPath;
- this.Document.height = this.Document[WidthSym]() * aspect;
- this.Document.nativeHeight = realsize.height;
- this.Document.nativeWidth = realsize.width;
- }
- }), 0);
- } else this._resized = imgPath;
- })
- .catch((err: any) => console.log(err));
- }
+ // _resized = "";
+ // resize = (imgPath: string) => {
+ // requestImageSize(imgPath)
+ // .then((size: any) => {
+ // const rotation = NumCast(this.dataDoc.rotation) % 180;
+ // const realsize = rotation === 90 || rotation === 270 ? { height: size.width, width: size.height } : size;
+ // const aspect = realsize.height / realsize.width;
+ // if (this.Document.width && (Math.abs(1 - NumCast(this.Document.height) / NumCast(this.Document.width) / (realsize.height / realsize.width)) > 0.1)) {
+ // setTimeout(action(() => {
+ // if (this.paths[NumCast(this.props.Document.curPage)] === imgPath && (!this.layoutDoc.isTemplateDoc || this.dataDoc !== this.layoutDoc)) {
+ // this._resized = imgPath;
+ // this.Document.height = this.Document[WidthSym]() * aspect;
+ // this.Document.nativeHeight = realsize.height;
+ // this.Document.nativeWidth = realsize.width;
+ // }
+ // }), 0);
+ // } else this._resized = imgPath;
+ // })
+ // .catch((err: any) => console.log(err));
+ // }
@action
onPointerEnter = () => {
@@ -306,7 +306,7 @@ export class ImageBox extends DocAnnotatableComponent<FieldViewProps, ImageDocum
const aspect = (rotation % 180) ? this.Document[HeightSym]() / this.Document[WidthSym]() : 1;
const shift = (rotation % 180) ? (nativeHeight - nativeWidth / aspect) / 2 : 0;
- !this.Document.ignoreAspect && this._resized !== srcpath && this.resize(srcpath);
+ // !this.Document.ignoreAspect && this._resized !== srcpath && this.resize(srcpath);
return <div className="imageBox-cont" key={this.props.Document[Id]} ref={this.createDropTarget} onContextMenu={this.specificContextMenu}>
<div className="imageBox-fader" >
diff --git a/src/server/ApiManagers/UploadManager.ts b/src/server/ApiManagers/UploadManager.ts
index 74f45ae62..583eaa59b 100644
--- a/src/server/ApiManagers/UploadManager.ts
+++ b/src/server/ApiManagers/UploadManager.ts
@@ -169,8 +169,7 @@ export default class UploadManager extends ApiManager {
secureHandler: async ({ req, res }) => {
const { source } = req.body;
if (typeof source === "string") {
- const { serverAccessPaths } = await DashUploadUtils.UploadImage(source);
- return res.send(await DashUploadUtils.InspectImage(serverAccessPaths[SizeSuffix.Original]));
+ return res.send(await DashUploadUtils.InspectImage(source));
}
res.send({});
}
diff --git a/src/server/DashUploadUtils.ts b/src/server/DashUploadUtils.ts
index 62a702a92..b826b4882 100644
--- a/src/server/DashUploadUtils.ts
+++ b/src/server/DashUploadUtils.ts
@@ -1,4 +1,4 @@
-import * as fs from 'fs';
+import { unlinkSync, createWriteStream, readFileSync, rename } from 'fs';
import { Utils } from '../Utils';
import * as path from 'path';
import * as sharp from 'sharp';
@@ -28,6 +28,10 @@ export function InjectSize(filename: string, size: SizeSuffix) {
return filename.substring(0, filename.length - extension.length) + size + extension;
}
+function isLocal() {
+ return /Dash-Web[\\\/]src[\\\/]server[\\\/]public[\\\/](.*)/;
+}
+
export namespace DashUploadUtils {
export interface Size {
@@ -93,12 +97,12 @@ export namespace DashUploadUtils {
}
async function UploadPdf(absolutePath: string) {
- const dataBuffer = fs.readFileSync(absolutePath);
+ const dataBuffer = readFileSync(absolutePath);
const result: ParsedPDF = await parse(dataBuffer);
const parsedName = basename(absolutePath);
await new Promise<void>((resolve, reject) => {
const textFilename = `${parsedName.substring(0, parsedName.length - 4)}.txt`;
- const writeStream = fs.createWriteStream(serverPathToFile(Directory.text, textFilename));
+ const writeStream = createWriteStream(serverPathToFile(Directory.text, textFilename));
writeStream.write(result.text, error => error ? reject(error) : resolve());
});
return MoveParsedFile(absolutePath, Directory.pdfs);
@@ -167,16 +171,22 @@ export namespace DashUploadUtils {
* @param source is the path or url to the image in question
*/
export const InspectImage = async (source: string): Promise<InspectionResults> => {
- const url = convert(source);
- const exifData = await parseExifData(url);
+ let resolvedUrl: string;
+ const matches = isLocal().exec(source);
+ if (matches === null) {
+ resolvedUrl = source;
+ } else {
+ resolvedUrl = `http://localhost:1050/${matches[1].split("\\").join("/")}`;
+ }
+ const exifData = await parseExifData(resolvedUrl);
const results = {
exifData,
- requestable: url
+ requestable: resolvedUrl
};
const { headers } = (await new Promise<any>((resolve, reject) => {
- request.head(url, (error, res) => error ? reject(error) : resolve(res));
+ request.head(resolvedUrl, (error, res) => error ? reject(error) : resolve(res));
}).catch(error => console.error(error)));
- const { width: nativeWidth, height: nativeHeight }: RequestedImageSize = await requestImageSize(url);
+ const { width: nativeWidth, height: nativeHeight }: RequestedImageSize = await requestImageSize(resolvedUrl);
return {
source,
contentSize: parseInt(headers[size]),
@@ -191,22 +201,20 @@ export namespace DashUploadUtils {
return new Promise<{ clientAccessPath: Opt<string> }>(resolve => {
const filename = basename(absolutePath);
const destinationPath = serverPathToFile(destination, filename);
- fs.rename(absolutePath, destinationPath, error => {
+ rename(absolutePath, destinationPath, error => {
resolve({ clientAccessPath: error ? undefined : clientPathToFile(destination, filename) });
});
});
}
export const UploadInspectedImage = async (metadata: InspectionResults, filename?: string, format?: string, prefix = ""): Promise<ImageUploadInformation> => {
- const { requestable, source, contentSize, contentType, exifData } = metadata;
+ const { requestable, source, ...remaining } = metadata;
const resolved = filename || generate(prefix, requestable);
const extension = format || sanitizeExtension(requestable || resolved);
const information: ImageUploadInformation = {
clientAccessPath: clientPathToFile(Directory.images, resolved),
serverAccessPaths: {},
- exifData,
- contentSize,
- contentType,
+ ...remaining
};
const { pngs, jpgs } = AcceptibleMedia;
return new Promise<ImageUploadInformation>(async (resolve, reject) => {
@@ -226,34 +234,22 @@ export namespace DashUploadUtils {
await new Promise<void>(resolve => {
const filename = InjectSize(resolved, suffix);
information.serverAccessPaths[suffix] = serverPathToFile(Directory.images, filename);
- request(requestable).pipe(resizer).pipe(fs.createWriteStream(serverPathToFile(Directory.images, filename)))
+ request(requestable).pipe(resizer).pipe(createWriteStream(serverPathToFile(Directory.images, filename)))
.on('close', resolve)
.on('error', reject);
});
}
- if (source.includes("Dash-Web")) {
- await new Promise<boolean>(resolve => {
- fs.unlink(source, error => resolve(error === null));
- });
+ if (isLocal().test(source)) {
+ unlinkSync(source);
}
resolve(information);
});
};
- const convert = (url: string) => {
- let resolved: string;
- const matches = /Dash-Web[\\\/]src[\\\/]server[\\\/]public[\\\/](.*)/.exec(url);
- if (matches === null) {
- resolved = url;
- } else {
- resolved = `http://localhost:1050/${matches[1].split("\\").join("/")}`;
- }
- return resolved;
- };
-
const parseExifData = async (source: string): Promise<EnrichedExifData> => {
+ const image = await request.get(source, { encoding: null });
return new Promise<EnrichedExifData>(resolve => {
- new ExifImage(source, (error, data) => {
+ new ExifImage({ image }, (error, data) => {
let reason: Opt<string> = undefined;
if (error) {
reason = (error as any).code;