From dd119f3c7dd3315bbf2e61060d6f7f825ea243e0 Mon Sep 17 00:00:00 2001 From: trickypr <23250792+trickypr@users.noreply.github.com> Date: Sun, 26 Nov 2023 21:05:16 +1100 Subject: [PATCH 1/5] Add `add-file` as an admin command --- src/server/admin-cli/admin.ts | 20 +++++++++++++-- src/server/admin-cli/operations.ts | 40 ++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/server/admin-cli/admin.ts b/src/server/admin-cli/admin.ts index 380fe8a..5d8a825 100644 --- a/src/server/admin-cli/admin.ts +++ b/src/server/admin-cli/admin.ts @@ -4,7 +4,8 @@ import { deleteUser, listUsers, deleteAllUsers, - changePassword + changePassword, + addFile } from './operations.js'; import { setupEnvironment } from '../core/environment/environment.js'; import { createDatabase } from '../core/database/database.js'; @@ -27,7 +28,8 @@ const runAdminCli = async () => { 'delete-user', 'list-users', 'delete-all-users', - 'change-password' + 'change-password', + 'add-file' ].includes(mode) === false ) { console.log(mode); @@ -83,6 +85,20 @@ const runAdminCli = async () => { const newPasswordRaw = process.argv[4]; await changePassword(username, newPasswordRaw); } + + if (mode === 'add-file') { + if (!process.argv[3] || !process.argv[4] || !process.argv[5]) { + console.log( + 'File path, file name and owner must be specified after mode arg. Exiting.' + ); + process.exit(1); + } + const filePath = process.argv[3]; + const fileName = process.argv[4]; + const ownerUsername = process.argv[5]; + + await addFile(filePath, fileName, ownerUsername); + } }; runAdminCli().catch((err) => { diff --git a/src/server/admin-cli/operations.ts b/src/server/admin-cli/operations.ts index 90d4ac6..1f11673 100644 --- a/src/server/admin-cli/operations.ts +++ b/src/server/admin-cli/operations.ts @@ -1,7 +1,12 @@ import bcrypt from 'bcryptjs'; -import { User } from '../models/User.js'; +import fs from 'fs'; +import path from 'path'; +import { v4 as uuid } from 'uuid'; +import type { CreationAttributes } from 'sequelize'; +import { User } from '../models/User.js'; import type { IUser, UserRole } from '../../shared/types.js'; +import { MediaItem } from '../models/MediaItem.js'; const createUser = (username: string, role: UserRole, passwordRaw: string) => { const user: IUser = {} as IUser; @@ -70,4 +75,35 @@ const changePassword = async (username: string, newPasswordRaw: string) => { await User.update({ password: newPasswordHashed }, { where: { username } }); }; -export { createUser, deleteUser, listUsers, deleteAllUsers, changePassword }; +const addFile = async (filePath: string, name: string, ownerName: string) => { + const url = path.resolve('data/uploads', filePath); + if (!fs.existsSync(url)) { + throw new Error(`File ${url} does not exist!`); + } + + const user = await User.findOne({ where: { username: ownerName } }); + + if (!user) { + throw new Error(`User ${ownerName} does not exist!`); + } + + const newMediaItem: CreationAttributes = { + id: uuid(), + type: 'file', + owner: user.id, + name, + url, + settings: {} + }; + + await MediaItem.create(newMediaItem); +}; + +export { + createUser, + deleteUser, + listUsers, + deleteAllUsers, + changePassword, + addFile +}; From da435762ce29cccfe8d1bfd14171eaf1d9e9f63f Mon Sep 17 00:00:00 2001 From: trickypr <23250792+trickypr@users.noreply.github.com> Date: Sun, 26 Nov 2023 21:14:01 +1100 Subject: [PATCH 2/5] Bug: Correct file url --- src/server/admin-cli/operations.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/server/admin-cli/operations.ts b/src/server/admin-cli/operations.ts index 1f11673..2a6c0bc 100644 --- a/src/server/admin-cli/operations.ts +++ b/src/server/admin-cli/operations.ts @@ -75,10 +75,10 @@ const changePassword = async (username: string, newPasswordRaw: string) => { await User.update({ password: newPasswordHashed }, { where: { username } }); }; -const addFile = async (filePath: string, name: string, ownerName: string) => { - const url = path.resolve('data/uploads', filePath); - if (!fs.existsSync(url)) { - throw new Error(`File ${url} does not exist!`); +const addFile = async (url: string, name: string, ownerName: string) => { + const fsPath = path.resolve('data/uploads', url); + if (!fs.existsSync(fsPath)) { + throw new Error(`File ${fsPath} does not exist!`); } const user = await User.findOne({ where: { username: ownerName } }); From 086ad44ea9c4b37b9c613548f3993d47ed320bdf Mon Sep 17 00:00:00 2001 From: trickypr <23250792+trickypr@users.noreply.github.com> Date: Sun, 26 Nov 2023 21:33:02 +1100 Subject: [PATCH 3/5] Add to list of commands --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 264ff39..a163faa 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,7 @@ You can run the following commands with a **preceding space**, preventing the pa - `delete-user ` - `delete-all-users` - `change-password ` +- `add-file ` ## Changelog From 817ccced7d722992f5034eda36dcb2df2084a256 Mon Sep 17 00:00:00 2001 From: trickypr <23250792+trickypr@users.noreply.github.com> Date: Fri, 12 Jan 2024 15:18:34 +1100 Subject: [PATCH 4/5] Prefix with uuid --- src/server/admin-cli/operations.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/server/admin-cli/operations.ts b/src/server/admin-cli/operations.ts index 2a6c0bc..7b02d1e 100644 --- a/src/server/admin-cli/operations.ts +++ b/src/server/admin-cli/operations.ts @@ -87,16 +87,25 @@ const addFile = async (url: string, name: string, ownerName: string) => { throw new Error(`User ${ownerName} does not exist!`); } + const id = uuid(); + const newFileName = `${id}-${url}`; + const newFsPath = path.resolve('data/uploads', newFileName); + fs.renameSync(fsPath, newFsPath); + console.log(`Media file at '${fsPath}' was renamed to '${newFileName}'`); + const newMediaItem: CreationAttributes = { id: uuid(), type: 'file', owner: user.id, name, - url, + url: newFileName, settings: {} }; - await MediaItem.create(newMediaItem); + const mediaItem = await MediaItem.create(newMediaItem); + console.log( + `'${mediaItem.name}' (${mediaItem.id}) was added with owner '${user.username}' (${user.id})` + ); }; export { From 648b0ed693c6b1fa809edb98994eb936957b47af Mon Sep 17 00:00:00 2001 From: trickypr <23250792+trickypr@users.noreply.github.com> Date: Fri, 12 Jan 2024 15:20:11 +1100 Subject: [PATCH 5/5] Update readme per comment --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a163faa..b3c2c60 100644 --- a/README.md +++ b/README.md @@ -185,7 +185,7 @@ You can run the following commands with a **preceding space**, preventing the pa - `delete-user ` - `delete-all-users` - `change-password ` -- `add-file ` +- `add-file <OWNER_USERNAME>` ## Changelog