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 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 { file } = await readBody<{ file: File }>(event)
111
+ const { files } = await readBody<{ files: File[] }>(event)
112
112
 
113
- await storeFileLocally(
114
- file.content, // the stringified version of the file
115
- 8, // you can add a name for the file or length of Unique ID that will be automatically generated!
116
- '/userFiles' // the folder the file will be stored in
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
- // {OR}
120
+ // {OR}
120
121
 
121
- // Parses a data URL and returns an object with the binary data and the file extension.
122
- const { binaryString, ext } = parseDataUrl(file.content)
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
@@ -2,6 +2,7 @@ import * as _nuxt_schema from '@nuxt/schema';
2
2
 
3
3
  interface ModuleOptions {
4
4
  mount: string;
5
+ version: string;
5
6
  }
6
7
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
7
8
 
package/dist/module.d.ts CHANGED
@@ -2,6 +2,7 @@ import * as _nuxt_schema from '@nuxt/schema';
2
2
 
3
3
  interface ModuleOptions {
4
4
  mount: string;
5
+ version: string;
5
6
  }
6
7
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
7
8
 
package/dist/module.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "nuxt-file-storage",
3
3
  "configKey": "fileStorage",
4
- "version": "0.2.3"
4
+ "version": "0.2.6"
5
5
  }
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
- // mount: 'userFiles',
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"));
@@ -2,6 +2,7 @@ export default function (): {
2
2
  files: import("vue").Ref<{
3
3
  content: any;
4
4
  name: string;
5
+ lastModified: string;
5
6
  readonly size: number;
6
7
  readonly type: string;
7
8
  arrayBuffer: () => Promise<ArrayBuffer>;
@@ -6,6 +6,10 @@ export default function() {
6
6
  reader.onload = (e) => {
7
7
  files.value.push({
8
8
  ...file,
9
+ name: file.name,
10
+ size: file.size,
11
+ type: file.type,
12
+ lastModified: file.lastModified,
9
13
  content: e.target.result
10
14
  });
11
15
  };
@@ -1,10 +1,20 @@
1
1
  /// <reference types="node" />
2
2
  /**
3
- * @returns mime type
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: (dataurl: string, fileNameOrIdLength: string | number, filelocation?: string) => Promise<string>;
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 (dataurl, fileNameOrIdLength, filelocation = "") => {
4
- const { binaryString, ext } = parseDataUrl(dataurl);
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 filename = typeof fileNameOrIdLength == "number" ? generateRandomId(fileNameOrIdLength) : fileNameOrIdLength;
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}.${ext}`, binaryString, {
9
+ await writeFile(`${location}${filelocation}/${filename}`, binaryString, {
9
10
  flag: "w"
10
11
  });
11
- return `${filename}.${ext}`;
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",
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",