From 602242d12c4aec63557f7aaf08a3ca8db637687e Mon Sep 17 00:00:00 2001 From: vovastelmashchuk Date: Thu, 24 Oct 2024 22:14:11 +0300 Subject: [PATCH] Change file type and small refaactor --- src/core/utils.js | 9 +++++++++ src/routers/admin/detail/file.js | 16 +++------------- src/templates/widgets/media_component.html | 4 +++- 3 files changed, 15 insertions(+), 14 deletions(-) create mode 100644 src/core/utils.js diff --git a/src/core/utils.js b/src/core/utils.js new file mode 100644 index 0000000..659dd39 --- /dev/null +++ b/src/core/utils.js @@ -0,0 +1,9 @@ +export function generateRandomString(count) { + let result = ''; + const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + const charactersLength = characters.length; + for (let i = 0; i < count; i++) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)) + } + return result +} diff --git a/src/routers/admin/detail/file.js b/src/routers/admin/detail/file.js index e9eced9..fc740e9 100644 --- a/src/routers/admin/detail/file.js +++ b/src/routers/admin/detail/file.js @@ -2,22 +2,12 @@ import fs from 'fs' import path from 'path' import { uploadFileFromPath } from "../../../minio/utils.js"; import { updateVideoPathBySlug } from '../../../core/episodeRepo.js'; +import { generateRandomString } from '../../../core/utils.js'; import dotenv from 'dotenv'; dotenv.config(); const startUrl = process.env.S3_START_URL; -function fileSuffix() { - const count = 64; - let result = ''; - const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; - const charactersLength = characters.length; - for (let i = 0; i < count; i++) { - result += characters.charAt(Math.floor(Math.random() * charactersLength)) - } - return result -} - async function uploadVideo(request, h) { try { const data = request.payload; @@ -26,7 +16,7 @@ async function uploadVideo(request, h) { const fileFormat = "mp4"; const showSlug = request.params.showSlug; const episodeSlug = request.params.episodeSlug; - const saveFileName = `${episodeSlug}-${fileSuffix()}.${fileFormat}`; + const saveFileName = `${showSlug}-${episodeSlug}.${fileFormat}`; const file = data.video; console.log(file); @@ -47,7 +37,7 @@ async function uploadVideo(request, h) { file.on('end', () => resolve()); }); - const s3FileKey = `${showSlug}/videos/${saveFileName}` + const s3FileKey = `${showSlug}/episodes/${episodeSlug}/video-${generateRandomString(32)}.${fileFormat}`; await uploadFileFromPath(s3FileKey, filePath, "video/mov"); await updateVideoPathBySlug(showSlug, episodeSlug, s3FileKey); diff --git a/src/templates/widgets/media_component.html b/src/templates/widgets/media_component.html index e15b3fa..1ebdd74 100644 --- a/src/templates/widgets/media_component.html +++ b/src/templates/widgets/media_component.html @@ -9,11 +9,13 @@ {{else}} + {{#if showVideo}} {{/if}} + {{/if}} {{#if showVideo}}
@@ -30,7 +32,7 @@
- +