-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12487ad
commit e8a1c75
Showing
5 changed files
with
25 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,26 @@ | ||
import { randomUUID } from 'node:crypto' | ||
import { type File } from 'core/contracts' | ||
import { mkdir } from 'node:fs/promises' | ||
import { existsSync, writeFileSync } from 'node:fs' | ||
import { existsSync, writeFileSync, mkdirSync } from 'node:fs' | ||
import { join } from 'node:path' | ||
|
||
export const uploadFiles = async (files: File[], destination: string): Promise<string[]> => { | ||
const promises = files.map(async (file) => { | ||
export const uploadFiles = async ( | ||
files: File[], | ||
destination: string, | ||
fileName?: string | ||
): Promise<string[]> => { | ||
const paths: string[] = [] | ||
for (const file of files) { | ||
const publicFolder = join(__dirname, '..', '..', '..', 'public') | ||
const extension = file.mimetype.split('/')[1] | ||
const fileName = `${randomUUID()}.${extension}` | ||
const path = join(publicFolder, destination, fileName) | ||
const isFolderExist = existsSync(join(publicFolder, destination)) | ||
if (!isFolderExist) { | ||
await mkdir(join(publicFolder, destination), { recursive: true }) | ||
const extension = file.mimetype.split('/')?.[1] | ||
const fileNameGeneratedOrProvided = fileName ?? `${randomUUID()}.${extension}` | ||
const path = join(publicFolder, destination, fileNameGeneratedOrProvided) | ||
const isFolderExists = existsSync(join(publicFolder, destination)) | ||
if (!isFolderExists) { | ||
mkdirSync(join(publicFolder, destination), { recursive: true }) | ||
} | ||
const buffer = file.data | ||
writeFileSync(path, buffer) | ||
return `${destination}/${fileName}` | ||
}) | ||
return await Promise.all(promises) | ||
paths.push(`${destination}/${fileNameGeneratedOrProvided}`) | ||
} | ||
return paths | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters