@strapi/provider-upload-local 4.10.0-beta.0 → 4.10.0-beta.1

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.
@@ -0,0 +1,36 @@
1
+ /// <reference types="node" />
2
+ import { ReadStream } from 'fs';
3
+ interface File {
4
+ name: string;
5
+ alternativeText?: string;
6
+ caption?: string;
7
+ width?: number;
8
+ height?: number;
9
+ formats?: Record<string, unknown>;
10
+ hash: string;
11
+ ext?: string;
12
+ mime: string;
13
+ size: number;
14
+ url: string;
15
+ previewUrl?: string;
16
+ path?: string;
17
+ provider?: string;
18
+ provider_metadata?: Record<string, unknown>;
19
+ stream?: ReadStream;
20
+ buffer?: Buffer;
21
+ }
22
+ interface InitOptions {
23
+ sizeLimit?: number;
24
+ }
25
+ interface CheckFileSizeOptions {
26
+ sizeLimit?: number;
27
+ }
28
+ declare const _default: {
29
+ init({ sizeLimit: providerOptionsSizeLimit }?: InitOptions): {
30
+ checkFileSize(file: File, options: CheckFileSizeOptions): void;
31
+ uploadStream(file: File): Promise<void>;
32
+ upload(file: File): Promise<void>;
33
+ delete(file: File): Promise<string | void>;
34
+ };
35
+ };
36
+ export = _default;
package/dist/index.js ADDED
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const stream_1 = require("stream");
6
+ const fs_1 = __importDefault(require("fs"));
7
+ const path_1 = __importDefault(require("path"));
8
+ const fs_extra_1 = __importDefault(require("fs-extra"));
9
+ const utils_1 = __importDefault(require("@strapi/utils"));
10
+ const { PayloadTooLargeError } = utils_1.default.errors;
11
+ const { kbytesToBytes, bytesToHumanReadable } = utils_1.default.file;
12
+ const UPLOADS_FOLDER_NAME = 'uploads';
13
+ module.exports = {
14
+ init({ sizeLimit: providerOptionsSizeLimit } = {}) {
15
+ // TODO V5: remove providerOptions sizeLimit
16
+ if (providerOptionsSizeLimit) {
17
+ process.emitWarning('[deprecated] In future versions, "sizeLimit" argument will be ignored from upload.config.providerOptions. Move it to upload.config');
18
+ }
19
+ // Ensure uploads folder exists
20
+ const uploadPath = path_1.default.resolve(strapi.dirs.static.public, UPLOADS_FOLDER_NAME);
21
+ if (!fs_extra_1.default.pathExistsSync(uploadPath)) {
22
+ throw new Error(`The upload folder (${uploadPath}) doesn't exist or is not accessible. Please make sure it exists.`);
23
+ }
24
+ return {
25
+ checkFileSize(file, options) {
26
+ const { sizeLimit } = options ?? {};
27
+ // TODO V5: remove providerOptions sizeLimit
28
+ if (providerOptionsSizeLimit) {
29
+ if (kbytesToBytes(file.size) > providerOptionsSizeLimit)
30
+ throw new PayloadTooLargeError(`${file.name} exceeds size limit of ${bytesToHumanReadable(providerOptionsSizeLimit)}.`);
31
+ }
32
+ else if (sizeLimit) {
33
+ if (kbytesToBytes(file.size) > sizeLimit)
34
+ throw new PayloadTooLargeError(`${file.name} exceeds size limit of ${bytesToHumanReadable(sizeLimit)}.`);
35
+ }
36
+ },
37
+ uploadStream(file) {
38
+ if (!file.stream) {
39
+ return Promise.reject(new Error('Missing file stream'));
40
+ }
41
+ const { stream } = file;
42
+ return new Promise((resolve, reject) => {
43
+ (0, stream_1.pipeline)(stream, fs_1.default.createWriteStream(path_1.default.join(uploadPath, `${file.hash}${file.ext}`)), (err) => {
44
+ if (err) {
45
+ return reject(err);
46
+ }
47
+ file.url = `/${UPLOADS_FOLDER_NAME}/${file.hash}${file.ext}`;
48
+ resolve();
49
+ });
50
+ });
51
+ },
52
+ upload(file) {
53
+ if (!file.buffer) {
54
+ return Promise.reject(new Error('Missing file buffer'));
55
+ }
56
+ const { buffer } = file;
57
+ return new Promise((resolve, reject) => {
58
+ // write file in public/assets folder
59
+ fs_1.default.writeFile(path_1.default.join(uploadPath, `${file.hash}${file.ext}`), buffer, (err) => {
60
+ if (err) {
61
+ return reject(err);
62
+ }
63
+ file.url = `/${UPLOADS_FOLDER_NAME}/${file.hash}${file.ext}`;
64
+ resolve();
65
+ });
66
+ });
67
+ },
68
+ delete(file) {
69
+ return new Promise((resolve, reject) => {
70
+ const filePath = path_1.default.join(uploadPath, `${file.hash}${file.ext}`);
71
+ if (!fs_1.default.existsSync(filePath)) {
72
+ resolve("File doesn't exist");
73
+ return;
74
+ }
75
+ // remove file from public/assets folder
76
+ fs_1.default.unlink(filePath, (err) => {
77
+ if (err) {
78
+ return reject(err);
79
+ }
80
+ resolve();
81
+ });
82
+ });
83
+ },
84
+ };
85
+ },
86
+ };
87
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA,mCAAkC;AAClC,4CAAoC;AACpC,gDAAwB;AACxB,wDAA2B;AAC3B,0DAAkC;AAsBlC,MAAM,EAAE,oBAAoB,EAAE,GAAG,eAAK,CAAC,MAAM,CAAC;AAC9C,MAAM,EAAE,aAAa,EAAE,oBAAoB,EAAE,GAAG,eAAK,CAAC,IAAI,CAAC;AAE3D,MAAM,mBAAmB,GAAG,SAAS,CAAC;AAUtC,iBAAS;IACP,IAAI,CAAC,EAAE,SAAS,EAAE,wBAAwB,KAAkB,EAAE;QAC5D,4CAA4C;QAC5C,IAAI,wBAAwB,EAAE;YAC5B,OAAO,CAAC,WAAW,CACjB,oIAAoI,CACrI,CAAC;SACH;QAED,+BAA+B;QAC/B,MAAM,UAAU,GAAG,cAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC;QAChF,IAAI,CAAC,kBAAG,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE;YACnC,MAAM,IAAI,KAAK,CACb,sBAAsB,UAAU,mEAAmE,CACpG,CAAC;SACH;QAED,OAAO;YACL,aAAa,CAAC,IAAU,EAAE,OAA6B;gBACrD,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,IAAI,EAAE,CAAC;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C,CAAC;YACL,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/provider-upload-local",
3
- "version": "4.10.0-beta.0",
3
+ "version": "4.10.0-beta.1",
4
4
  "description": "Local provider for strapi upload",
5
5
  "keywords": [
6
6
  "upload",
@@ -27,20 +27,33 @@
27
27
  "url": "https://strapi.io"
28
28
  }
29
29
  ],
30
- "main": "./lib",
31
- "directories": {
32
- "lib": "./lib"
33
- },
30
+ "main": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "files": [
33
+ "./dist"
34
+ ],
34
35
  "scripts": {
35
- "test": "echo \"no tests yet\""
36
+ "build": "run -T tsc",
37
+ "build:ts": "run -T tsc",
38
+ "watch": "run -T tsc -w --preserveWatchOutput",
39
+ "clean": "run -T rimraf ./dist",
40
+ "prepublishOnly": "yarn clean && yarn build",
41
+ "test:unit": "run -T jest",
42
+ "test:unit:watch": "run -T jest --watch",
43
+ "lint": "run -T eslint ."
36
44
  },
37
45
  "dependencies": {
38
- "@strapi/utils": "4.10.0-beta.0",
46
+ "@strapi/utils": "4.10.0-beta.1",
39
47
  "fs-extra": "10.0.0"
40
48
  },
49
+ "devDependencies": {
50
+ "@types/jest": "29.2.0",
51
+ "eslint-config-custom": "4.10.0-beta.1",
52
+ "tsconfig": "4.10.0-beta.1"
53
+ },
41
54
  "engines": {
42
55
  "node": ">=14.19.1 <=18.x.x",
43
56
  "npm": ">=6.0.0"
44
57
  },
45
- "gitHead": "1519ef0e56d27b738f24fc88223797651ad47aaf"
58
+ "gitHead": "95d581b31bee464af42e5d8db408fa578d8532c7"
46
59
  }
package/lib/index.js DELETED
@@ -1,105 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * Module dependencies
5
- */
6
-
7
- // Public node modules.
8
- const { pipeline } = require('stream');
9
- const fs = require('fs');
10
- const path = require('path');
11
- const fse = require('fs-extra');
12
- const {
13
- errors: { PayloadTooLargeError },
14
- file: { kbytesToBytes, bytesToHumanReadable },
15
- } = require('@strapi/utils');
16
-
17
- const UPLOADS_FOLDER_NAME = 'uploads';
18
-
19
- module.exports = {
20
- init({ sizeLimit: providerOptionsSizeLimit } = {}) {
21
- // TODO V5: remove providerOptions sizeLimit
22
- if (providerOptionsSizeLimit) {
23
- process.emitWarning(
24
- `[deprecated] In future versions, "sizeLimit" argument will be ignored from upload.config.providerOptions. Move it to upload.config`
25
- );
26
- }
27
-
28
- // Ensure uploads folder exists
29
- const uploadPath = path.resolve(strapi.dirs.static.public, UPLOADS_FOLDER_NAME);
30
- if (!fse.pathExistsSync(uploadPath)) {
31
- throw new Error(
32
- `The upload folder (${uploadPath}) doesn't exist or is not accessible. Please make sure it exists.`
33
- );
34
- }
35
-
36
- return {
37
- checkFileSize(file, { sizeLimit } = {}) {
38
- // TODO V5: remove providerOptions sizeLimit
39
- if (providerOptionsSizeLimit) {
40
- if (kbytesToBytes(file.size) > providerOptionsSizeLimit)
41
- throw new PayloadTooLargeError(
42
- `${file.name} exceeds size limit of ${bytesToHumanReadable(
43
- providerOptionsSizeLimit
44
- )}.`
45
- );
46
- } else if (sizeLimit) {
47
- if (kbytesToBytes(file.size) > sizeLimit)
48
- throw new PayloadTooLargeError(
49
- `${file.name} exceeds size limit of ${bytesToHumanReadable(sizeLimit)}.`
50
- );
51
- }
52
- },
53
- uploadStream(file) {
54
- return new Promise((resolve, reject) => {
55
- pipeline(
56
- file.stream,
57
- fs.createWriteStream(path.join(uploadPath, `${file.hash}${file.ext}`)),
58
- (err) => {
59
- if (err) {
60
- return reject(err);
61
- }
62
-
63
- file.url = `/${UPLOADS_FOLDER_NAME}/${file.hash}${file.ext}`;
64
-
65
- resolve();
66
- }
67
- );
68
- });
69
- },
70
- upload(file) {
71
- return new Promise((resolve, reject) => {
72
- // write file in public/assets folder
73
- fs.writeFile(path.join(uploadPath, `${file.hash}${file.ext}`), file.buffer, (err) => {
74
- if (err) {
75
- return reject(err);
76
- }
77
-
78
- file.url = `/${UPLOADS_FOLDER_NAME}/${file.hash}${file.ext}`;
79
-
80
- resolve();
81
- });
82
- });
83
- },
84
- delete(file) {
85
- return new Promise((resolve, reject) => {
86
- const filePath = path.join(uploadPath, `${file.hash}${file.ext}`);
87
-
88
- if (!fs.existsSync(filePath)) {
89
- // eslint-disable-next-line no-promise-executor-return
90
- return resolve("File doesn't exist");
91
- }
92
-
93
- // remove file from public/assets folder
94
- fs.unlink(filePath, (err) => {
95
- if (err) {
96
- return reject(err);
97
- }
98
-
99
- resolve();
100
- });
101
- });
102
- },
103
- };
104
- },
105
- };