Skip to content

Commit

Permalink
Change file type and small refaactor
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Oct 24, 2024
1 parent 053f720 commit 602242d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
9 changes: 9 additions & 0 deletions src/core/utils.js
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 3 additions & 13 deletions src/routers/admin/detail/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion src/templates/widgets/media_component.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
</div>
</div>
{{else}}
{{#if showVideo}}
<button hx-post="/admin/show/{{showSlug}}/episode/{{episodeSlug}}/buildAudio" hx-swap="outerHTML"
hx-target="#media-container" class="common-button w-full px-4 py-2 my-2 transition-all duration-300 ease-in-out">
Build Audio
</button>
{{/if}}
{{/if}}

{{#if showVideo}}
<div class="w-full py-4">
Expand All @@ -30,7 +32,7 @@
<div class="w-full py-4">
<form action="{{uploadUrl}}" method="POST" enctype="multipart/form-data" hx-post="{{uploadUrl}}" hx-swap="outerHTML"
hx-target="#media-container">
<input type="file" name="video" accept=".mov" required />
<input type="file" name="video" accept=".mp4" required />
<button type="submit" class="common-button w-full px-4 py-2 my-2 transition-all duration-300 ease-in-out">Upload
Video</button>
</form>
Expand Down

0 comments on commit 602242d

Please sign in to comment.