nuxt-file-storage 0.2.3 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -9
- package/dist/module.d.mts +1 -0
- package/dist/module.d.ts +1 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +16 -2
- package/dist/runtime/composables/useFileStorage.d.ts +1 -0
- package/dist/runtime/composables/useFileStorage.mjs +4 -0
- package/dist/runtime/server/utils/storage.d.ts +13 -2
- package/dist/runtime/server/utils/storage.mjs +6 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -108,18 +108,20 @@ const submit = async () => {
|
|
|
108
108
|
using Nitro Server Engine, we will make an api route that recieves the files and stores them in the folder `userFiles`
|
|
109
109
|
```ts
|
|
110
110
|
export default defineEventHandler(async (event) => {
|
|
111
|
-
const {
|
|
111
|
+
const { files } = await readBody<{ files: File[] }>(event)
|
|
112
112
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
113
|
+
for ( const file of files ) {
|
|
114
|
+
await storeFileLocally(
|
|
115
|
+
file, // the file object
|
|
116
|
+
8, // you can add a name for the file or length of Unique ID that will be automatically generated!
|
|
117
|
+
'/userFiles' // the folder the file will be stored in
|
|
118
|
+
)
|
|
118
119
|
|
|
119
|
-
|
|
120
|
+
// {OR}
|
|
120
121
|
|
|
121
|
-
|
|
122
|
-
|
|
122
|
+
// Parses a data URL and returns an object with the binary data and the file extension.
|
|
123
|
+
const { binaryString, ext } = parseDataUrl(file.content)
|
|
124
|
+
}
|
|
123
125
|
|
|
124
126
|
return 'success!'
|
|
125
127
|
})
|
package/dist/module.d.mts
CHANGED
package/dist/module.d.ts
CHANGED
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { defineNuxtModule, createResolver, addImportsDir, addServerScanDir } from '@nuxt/kit';
|
|
1
|
+
import { defineNuxtModule, logger, createResolver, addImportsDir, addServerScanDir } from '@nuxt/kit';
|
|
2
2
|
import defu from 'defu';
|
|
3
3
|
|
|
4
|
+
const version = "0.2.6";
|
|
5
|
+
|
|
4
6
|
const module = defineNuxtModule({
|
|
5
7
|
meta: {
|
|
6
8
|
name: "nuxt-file-storage",
|
|
@@ -9,13 +11,25 @@ const module = defineNuxtModule({
|
|
|
9
11
|
//? Default configuration options of the Nuxt module
|
|
10
12
|
//! no defaults for now
|
|
11
13
|
// defaults: {
|
|
12
|
-
//
|
|
14
|
+
// version: '0.0.0',
|
|
13
15
|
// },
|
|
14
16
|
setup(options, nuxt) {
|
|
15
17
|
const config = nuxt.options.runtimeConfig;
|
|
16
18
|
config.public.fileStorage = defu(config.public.fileStorage, {
|
|
17
19
|
...options
|
|
18
20
|
});
|
|
21
|
+
if (nuxt.options.dev) {
|
|
22
|
+
//! I couldn't find a way to detect new updates to warn one time so it will warn every time the server runs again, if you know how to warn about a breaking change only the first run after an update feel free to open a new pull request
|
|
23
|
+
const previousVersion = config.public.fileStorage?.version;
|
|
24
|
+
if (!previousVersion || previousVersion !== version) {
|
|
25
|
+
logger.warn(
|
|
26
|
+
`There is a breaking change in the \`fileStoreLocally\` method, link to changelog:
|
|
27
|
+
https://github.com/NyllRE/nuxt-file-storage/releases/tag/v${version}
|
|
28
|
+
`
|
|
29
|
+
);
|
|
30
|
+
config.public.fileStorage.version = version;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
19
33
|
const resolve = createResolver(import.meta.url).resolve;
|
|
20
34
|
addImportsDir(resolve("runtime/composables"));
|
|
21
35
|
addServerScanDir(resolve("./runtime/server"));
|
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* #### Will store the file in the specified directory
|
|
4
|
+
* @returns file name: `${filename}`.`${fileExtension}`
|
|
5
|
+
* @prop file: provide the file object
|
|
4
6
|
* @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
|
|
7
|
+
* @prop filelocation: provide the folder you wish to locate the file in
|
|
5
8
|
*/
|
|
6
|
-
export declare const storeFileLocally: (
|
|
9
|
+
export declare const storeFileLocally: (file: File, fileNameOrIdLength: string | number, filelocation?: string) => Promise<string>;
|
|
7
10
|
export declare const deleteFile: (filename: string, filelocation?: string) => Promise<void>;
|
|
11
|
+
interface File {
|
|
12
|
+
name: string;
|
|
13
|
+
content: string;
|
|
14
|
+
size: string;
|
|
15
|
+
type: string;
|
|
16
|
+
lastModified: string;
|
|
17
|
+
}
|
|
8
18
|
/**
|
|
9
19
|
Parses a data URL and returns an object with the binary data and the file extension.
|
|
10
20
|
@param {string} file - The data URL
|
|
@@ -14,3 +24,4 @@ export declare const parseDataUrl: (file: string) => {
|
|
|
14
24
|
binaryString: Buffer;
|
|
15
25
|
ext: string;
|
|
16
26
|
};
|
|
27
|
+
export {};
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { writeFile, rm, mkdir } from "fs/promises";
|
|
2
2
|
import { useRuntimeConfig } from "#imports";
|
|
3
|
-
export const storeFileLocally = async (
|
|
4
|
-
const { binaryString, ext } = parseDataUrl(
|
|
3
|
+
export const storeFileLocally = async (file, fileNameOrIdLength, filelocation = "") => {
|
|
4
|
+
const { binaryString, ext } = parseDataUrl(file.content);
|
|
5
5
|
const location = useRuntimeConfig().public.fileStorage.mount;
|
|
6
|
-
const
|
|
6
|
+
const originalExt = file.name.toString().split(".").pop() || ext;
|
|
7
|
+
const filename = typeof fileNameOrIdLength == "number" ? `${generateRandomId(fileNameOrIdLength)}.${originalExt}` : `${fileNameOrIdLength}.${originalExt}`;
|
|
7
8
|
await mkdir(`${location}${filelocation}`, { recursive: true });
|
|
8
|
-
await writeFile(`${location}${filelocation}/${filename}
|
|
9
|
+
await writeFile(`${location}${filelocation}/${filename}`, binaryString, {
|
|
9
10
|
flag: "w"
|
|
10
11
|
});
|
|
11
|
-
return
|
|
12
|
+
return filename;
|
|
12
13
|
};
|
|
13
14
|
export const deleteFile = async (filename, filelocation = "") => {
|
|
14
15
|
const location = useRuntimeConfig().public.fileStorage.mount;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-file-storage",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "Easy solution to store files in your nuxt apps. Be able to upload files from the frontend and recieve them from the backend to then save the files in your project.",
|
|
5
5
|
"repository": "NyllRE/nuxt-file-storage",
|
|
6
6
|
"license": "MIT",
|