diff options
-rw-r--r-- | src/server/DashUploadUtils.ts | 38 | ||||
-rw-r--r-- | src/server/index.ts | 5 |
2 files changed, 35 insertions, 8 deletions
diff --git a/src/server/DashUploadUtils.ts b/src/server/DashUploadUtils.ts index 66874e96c..04a3559dc 100644 --- a/src/server/DashUploadUtils.ts +++ b/src/server/DashUploadUtils.ts @@ -1,7 +1,6 @@ import * as fs from 'fs'; import { Utils } from '../Utils'; import * as path from 'path'; -import { Opt } from '../new_fields/Doc'; import * as sharp from 'sharp'; import request = require('request-promise'); @@ -39,6 +38,28 @@ export namespace DashUploadUtils { const generate = (prefix: string, url: string) => `${prefix}upload_${Utils.GenerateGuid()}${path.extname(url).toLowerCase()}`; const sanitize = (filename: string) => filename.replace(/\s+/g, "_"); + /** + * Uploads an image specified by the @param source to Dash's /public/files/ + * directory, and returns information generated during that upload + * + * @param {string} source is either the absolute path of an already uploaded image or + * the url of a remote image + * @param {string} filename dictates what to call the image. If not specified, + * the name {@param prefix}_upload_{GUID} + * @param {string} prefix is a string prepended to the generated image name in the + * event that @param filename is not specified + * + * @returns {UploadInformation} This method returns + * 1) the paths to the uploaded image + * 2) the file name of each of the resized images + * 3) the size of the image, in bytes (4432130) + * 4) the content type of the image (jpg | png | etc.) + */ + export const UploadImage = async (source: string, filename?: string, prefix: string = ""): Promise<UploadInformation> => { + const metadata = await InspectImage(source); + return UploadInspectedImage(metadata, filename, prefix); + }; + export interface InspectionResults { isLocal: boolean; stream: any; @@ -47,18 +68,25 @@ export namespace DashUploadUtils { contentType?: string; } - export const InspectImage = async (url: string): Promise<InspectionResults> => { - const { isLocal, stream, normalized: normalizedUrl } = classify(url); + /** + * Based on the url's classification as local or remote, gleans + * as much information as possible about the specified image + * + * @param source is the path or url to the image in question + */ + export const InspectImage = async (source: string): Promise<InspectionResults> => { + const { isLocal, stream, normalized: normalizedUrl } = classify(source); const results = { isLocal, stream, normalizedUrl }; + // stop here if local, since request.head() can't handle local paths, only urls on the web if (isLocal) { return results; } const metadata = (await new Promise<any>((resolve, reject) => { - request.head(url, async (error, res) => { + request.head(source, async (error, res) => { if (error) { return reject(error); } @@ -72,7 +100,7 @@ export namespace DashUploadUtils { }; }; - export const UploadImage = async (metadata: InspectionResults, filename?: string, prefix = ""): Promise<Opt<UploadInformation>> => { + export const UploadInspectedImage = async (metadata: InspectionResults, filename?: string, prefix = ""): Promise<UploadInformation> => { const { isLocal, stream, normalizedUrl, contentSize, contentType } = metadata; const resolved = filename ? sanitize(filename) : generate(prefix, normalizedUrl); let extension = path.extname(normalizedUrl) || path.extname(resolved); diff --git a/src/server/index.ts b/src/server/index.ts index ecfeaa5fa..3da726cfa 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -582,8 +582,7 @@ app.post( for (const key in files) { const { type, path: location, name } = files[key]; const filename = path.basename(location); - const metadata = await DashUploadUtils.InspectImage(uploadDirectory + filename); - await DashUploadUtils.UploadImage(metadata, filename).catch(() => console.log(`Unable to process ${filename}`)); + await DashUploadUtils.UploadImage(uploadDirectory + filename, filename).catch(() => console.log(`Unable to process ${filename}`)); results.push({ name, type, path: `/files/${filename}` }); } _success(res, results); @@ -907,7 +906,7 @@ app.post(RouteStore.googlePhotosMediaDownload, async (req, res) => { const { contentSize, ...attributes } = await DashUploadUtils.InspectImage(item.baseUrl); const found: Opt<DashUploadUtils.UploadInformation> = await Database.Auxiliary.QueryUploadHistory(contentSize!); if (!found) { - const upload = await DashUploadUtils.UploadImage({ contentSize, ...attributes }, item.filename, prefix).catch(error => _error(res, downloadError, error)); + const upload = await DashUploadUtils.UploadInspectedImage({ contentSize, ...attributes }, item.filename, prefix).catch(error => _error(res, downloadError, error)); if (upload) { completed.push(upload); await Database.Auxiliary.LogUpload(upload); |