Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

export different file types #26

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<!-- <input type="file" id="images" accept="image/*" required> -->
<input
id="file-input"
:ref="fileInput"
ref="fileInput"
type="file"
name="files[]"
multiple
Expand All @@ -39,7 +39,7 @@
<p>{{ approveUpload }}</p>
</div>
<div class="images">
<img v-for="file in files" :key="file.name" :src="file.content" alt="file.name" />
<img v-for="file in files" :key="file.name" :src="(file.content as string)" alt="file.name" />
</div>
</div>
</div>
Expand Down
12 changes: 3 additions & 9 deletions playground/server/api/files.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import type { ServerFile } from '../../../src/types'

export default defineEventHandler(async (event) => {
const { files } = await readBody<{ files: File[] }>(event)
const { files } = await readBody<{ files: ServerFile[] }>(event)
const fileNames: string[] = []
for (const file of files) {
fileNames.push(await storeFileLocally(file, 12, '/specificFolder'))
}
return fileNames
})

interface File {
name: string
content: string
size: string
type: string
lastModified: string
}
7 changes: 2 additions & 5 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ import {
import defu from 'defu'
import { version } from '../package.json'

//? Module options TypeScript interface definition
export interface ModuleOptions {
mount: string
version: string
}
import type { ModuleOptions } from './types'
export type * from './types'

export default defineNuxtModule<ModuleOptions>({
meta: {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/composables/useFileStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ref } from 'vue'
import type { ClientFile } from '../../types'

export default function () {
const files = ref<ClientFile[]>([])
Expand All @@ -24,7 +25,6 @@ export default function () {
}

const handleFileInput = async (event: any) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Removed files.value.splice(0) could lead to file accumulation

Without clearing the files array, multiple file selections will keep adding to the array, potentially causing memory issues.

files.value.splice(0)

const promises = []
for (const file of event.target.files) {
Expand Down
14 changes: 10 additions & 4 deletions src/runtime/server/utils/storage.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { writeFile, rm, mkdir } from 'fs/promises'
import { useRuntimeConfig } from '#imports'
import type { ServerFile } from '../../../types'

/**
* #### Will store the file in the specified directory
* @returns file name: `${filename}`.`${fileExtension}`
* @prop file: provide the file object
* @prop fileNameOrIdLength: you can pass a string or a number, if you enter a string it will be the file name, if you enter a number it will generate a unique ID
* @prop filelocation: provide the folder you wish to locate the file in
* @prop `file`: provide the file object
* @prop `fileNameOrIdLength`: you can pass a string or a number, if you enter a string it will be the file name, if you enter a number it will generate a unique ID
* @prop `filelocation`: provide the folder you wish to locate the file in
*/
export const storeFileLocally = async (
file: ServerFile,
Expand All @@ -26,13 +27,18 @@ export const storeFileLocally = async (

await mkdir(`${location}${filelocation}`, { recursive: true })

await writeFile(`${location}${filelocation}/${filename}`, binaryString, {
await writeFile(`${location}${filelocation}/${filename}`, binaryString as any, {
flag: 'w',
})

return filename
}

/**
*
* @param `filename`: the name of the file you want to delete
* @param `filelocation`: the folder where the file is located, if it is in the root folder you can leave it empty, if it is in a subfolder you can pass the name of the subfolder with a preceding slash: `/subfolder`
*/
export const deleteFile = async (filename: string, filelocation: string = '') => {
const location = useRuntimeConfig().public.fileStorage.mount
await rm(`${location}${filelocation}/${filename}`)
Expand Down
18 changes: 18 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export interface ServerFile {
name: string
content: string
size: string
type: string
lastModified: string
}

export interface ClientFile extends Blob {
content: string | ArrayBuffer | null | undefined
name: string
lastModified: number
}

export interface ModuleOptions {
mount: string
version: string
}
13 changes: 0 additions & 13 deletions types/types.d.ts

This file was deleted.