From 30ff3dbdcd372208bd439e087610a5b72ca90a52 Mon Sep 17 00:00:00 2001 From: Holly Date: Sun, 26 Jan 2025 10:34:56 +0000 Subject: [PATCH] fixed uploads --- controllers/HomeController.ts | 5 ++- index.ts | 7 +++- models/home/DashboardViewModel.ts | 4 +- objects/HashFS.ts | 70 ++++++++++++++++--------------- services/DomainService.ts | 13 ++++++ utilities/ArrayUtility.ts | 10 +++++ views/home/dashboard.ejs | 23 ++++++---- 7 files changed, 86 insertions(+), 46 deletions(-) create mode 100644 services/DomainService.ts create mode 100644 utilities/ArrayUtility.ts diff --git a/controllers/HomeController.ts b/controllers/HomeController.ts index cf24db2..64ea474 100644 --- a/controllers/HomeController.ts +++ b/controllers/HomeController.ts @@ -1,12 +1,15 @@ import Controller from "./Controller"; import type DashboardViewModel from "../models/home/DashboardViewModel"; import UserService from "../services/UserService"; +import DomainService from "../services/DomainService"; +import ArrayUtility from "../utilities/ArrayUtility"; export default class HomeController extends Controller { public async Index_Get_AllowAnonymous() { if (this.session) { const dashboardViewModel: DashboardViewModel = { - recentUploads: await UserService.GetRecentUploads(this.session.userId) + recentUploads: await UserService.GetRecentUploads(this.session.userId), + domains: ArrayUtility.ToIdKeyedDict(await DomainService.LoadDomains()) } return this.view("dashboard",dashboardViewModel); diff --git a/index.ts b/index.ts index 133cb62..90345d5 100644 --- a/index.ts +++ b/index.ts @@ -35,7 +35,12 @@ fastify.register(FastifyView, { fastify.register(FastifyFormBody); -fastify.register(FastifyMultipart); +fastify.register(FastifyMultipart, { + limits: { + files: 1, + fileSize: 104857600 + } +}); fastify.register(FastifyCookie, { secret: Config.session.secret, diff --git a/models/home/DashboardViewModel.ts b/models/home/DashboardViewModel.ts index 27ba352..1c9ec84 100644 --- a/models/home/DashboardViewModel.ts +++ b/models/home/DashboardViewModel.ts @@ -1,5 +1,7 @@ +import type Domain from "../../entities/Domain"; import Media from "../../entities/Media"; export default interface DashboardViewModel { - recentUploads: Array + recentUploads: Array, + domains: { [key: string]: Domain } } \ No newline at end of file diff --git a/objects/HashFS.ts b/objects/HashFS.ts index ac5c67f..7ea5fdd 100644 --- a/objects/HashFS.ts +++ b/objects/HashFS.ts @@ -1,13 +1,14 @@ // ! Hashed File Store (not file system!!) import { join } from "path"; -import { existsSync, mkdirSync, createWriteStream, rename, stat, writeFile, rm, rmSync, } from "fs"; +import { existsSync, mkdirSync, createWriteStream, rename, stat, writeFile, rm, rmSync, createReadStream } from "fs"; import { Console } from "hsconsole"; import { yellow } from "dyetty"; import { createHash, randomBytes } from "crypto"; import FunkyArray from "funky-array"; import HashFSFileInformation from "./HashFSFileInformation"; import type { BusboyFileStream } from "@fastify/busboy"; +import { pipeline } from "stream/promises"; export default class HashFS { public static STARTUP_DIR: string; @@ -104,49 +105,50 @@ export default class HashFS { if (err) { return reject(err); } - + resolve(fileInfo); }) }); } - + public AddFromStream(stream: BusboyFileStream) { return new Promise(async (resolve, reject) => { const hasher = createHash("sha1"); hasher.setEncoding("hex"); - const tempFilePath = join(this.tempPath, randomBytes(16).toString("base64url")); - const tempFile = createWriteStream(tempFilePath); - tempFile.on("close", async () => { - const hash: string = hasher.read(); - const fileInfo = new HashFSFileInformation(); - fileInfo.fileHash = hash; - fileInfo.fileSize = fileSize; - if (await this.FileExists(hash)) { - fileInfo.fileExistsAlready = true; - rm(tempFilePath, err => { - if (err) { - return reject(err); - } - - this.logInfo(`File with hash "${hash}" already exists.`); - resolve(fileInfo); - }); - } else { - rename(tempFilePath, this.GetFilePath(hash), err => { - if (err) { - return reject(err); - } - - this.logInfo(`Stored file as ${hash}`); - resolve(fileInfo); - }); - } - }); - stream.pipe(tempFile); - stream.pipe(hasher); let fileSize = 0; - stream.on("data", chunk => fileSize += chunk.length); + const tempFilePath = join(this.tempPath, randomBytes(16).toString("base64url")); + await pipeline(stream, createWriteStream(tempFilePath)); + const readStream = createReadStream(tempFilePath); + for await (const chunk of readStream) { + fileSize += chunk.length; + hasher.write(chunk) + } + hasher.end(); + const hash: string = hasher.read(); + const fileInfo = new HashFSFileInformation(); + fileInfo.fileHash = hash; + fileInfo.fileSize = fileSize; + if (await this.FileExists(hash)) { + fileInfo.fileExistsAlready = true; + rm(tempFilePath, err => { + if (err) { + return reject(err); + } + + this.logInfo(`File with hash "${hash}" already exists.`); + resolve(fileInfo); + }); + } else { + rename(tempFilePath, this.GetFilePath(hash), err => { + if (err) { + return reject(err); + } + + this.logInfo(`Stored file as ${hash}`); + resolve(fileInfo); + }); + } }); } diff --git a/services/DomainService.ts b/services/DomainService.ts new file mode 100644 index 0000000..356ee69 --- /dev/null +++ b/services/DomainService.ts @@ -0,0 +1,13 @@ +import { Console } from "hsconsole"; +import DomainRepo from "../repos/DomainRepo"; + +export default abstract class DomainService { + public static async LoadDomains() { + try { + return await DomainRepo.SelectAll(); + } catch (e) { + Console.printError(`EUS server service error:\n${e}`); + throw e; + } + } +} \ No newline at end of file diff --git a/utilities/ArrayUtility.ts b/utilities/ArrayUtility.ts new file mode 100644 index 0000000..b5438d0 --- /dev/null +++ b/utilities/ArrayUtility.ts @@ -0,0 +1,10 @@ +export default abstract class ArrayUtility { + public static ToIdKeyedDict(array: Array) { + const dict: { [key: string]: any } = {}; + for (const item of array) { + dict[item.Id] = item; + } + + return dict; + } +} \ No newline at end of file diff --git a/views/home/dashboard.ejs b/views/home/dashboard.ejs index dbd36c9..02c5d49 100644 --- a/views/home/dashboard.ejs +++ b/views/home/dashboard.ejs @@ -11,15 +11,20 @@
- - - <% for (const upload of recentUploads) { %> - - - - <% } %> - -
<%= upload.FileName %>
+
+ <% for (const upload of recentUploads) { %> +
+
+
+ <% if (upload.MediaType.startsWith("image/")) { %> + ://<%= domains[upload.DomainId].Domain %>/<%= upload.MediaTag %>" height="30" width="50"> + <% } %> +
+
<%= upload.FileName %>
+
+
+ <% } %> +