@strapi/provider-upload-local 4.1.6-alpha.0 → 4.1.6-alpha.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.
Files changed (2) hide show
  1. package/lib/index.js +20 -14
  2. package/package.json +4 -3
package/lib/index.js CHANGED
@@ -8,8 +8,11 @@
8
8
  const { pipeline } = require('stream');
9
9
  const fs = require('fs');
10
10
  const path = require('path');
11
+ const fse = require('fs-extra');
11
12
  const { PayloadTooLargeError } = require('@strapi/utils').errors;
12
13
 
14
+ const UPLOADS_FOLDER_NAME = 'uploads';
15
+
13
16
  module.exports = {
14
17
  init({ sizeLimit = 1000000 } = {}) {
15
18
  const verifySize = file => {
@@ -18,7 +21,13 @@ module.exports = {
18
21
  }
19
22
  };
20
23
 
21
- const publicDir = strapi.dirs.public;
24
+ // Ensure uploads folder exists
25
+ const uploadPath = path.resolve(strapi.dirs.public, UPLOADS_FOLDER_NAME);
26
+ if (!fse.pathExistsSync(uploadPath)) {
27
+ throw new Error(
28
+ `The upload folder (${uploadPath}) doesn't exist or is not accessible. Please make sure it exists.`
29
+ );
30
+ }
22
31
 
23
32
  return {
24
33
  uploadStream(file) {
@@ -27,7 +36,7 @@ module.exports = {
27
36
  return new Promise((resolve, reject) => {
28
37
  pipeline(
29
38
  file.stream,
30
- fs.createWriteStream(path.join(publicDir, `/uploads/${file.hash}${file.ext}`)),
39
+ fs.createWriteStream(path.join(uploadPath, `${file.hash}${file.ext}`)),
31
40
  err => {
32
41
  if (err) {
33
42
  return reject(err);
@@ -44,24 +53,21 @@ module.exports = {
44
53
  verifySize(file);
45
54
 
46
55
  return new Promise((resolve, reject) => {
47
- fs.writeFile(
48
- path.join(publicDir, `/uploads/${file.hash}${file.ext}`),
49
- file.buffer,
50
- err => {
51
- if (err) {
52
- return reject(err);
53
- }
56
+ // write file in public/assets folder
57
+ fs.writeFile(path.join(uploadPath, `${file.hash}${file.ext}`), file.buffer, err => {
58
+ if (err) {
59
+ return reject(err);
60
+ }
54
61
 
55
- file.url = `/uploads/${file.hash}${file.ext}`;
62
+ file.url = `/${UPLOADS_FOLDER_NAME}/${file.hash}${file.ext}`;
56
63
 
57
- resolve();
58
- }
59
- );
64
+ resolve();
65
+ });
60
66
  });
61
67
  },
62
68
  delete(file) {
63
69
  return new Promise((resolve, reject) => {
64
- const filePath = path.join(publicDir, `/uploads/${file.hash}${file.ext}`);
70
+ const filePath = path.join(uploadPath, `${file.hash}${file.ext}`);
65
71
 
66
72
  if (!fs.existsSync(filePath)) {
67
73
  return resolve("File doesn't exist");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/provider-upload-local",
3
- "version": "4.1.6-alpha.0",
3
+ "version": "4.1.6-alpha.1",
4
4
  "description": "Local provider for strapi upload",
5
5
  "keywords": [
6
6
  "upload",
@@ -35,11 +35,12 @@
35
35
  "test": "echo \"no tests yet\""
36
36
  },
37
37
  "dependencies": {
38
- "@strapi/utils": "4.1.6-alpha.0"
38
+ "@strapi/utils": "4.1.6-alpha.1",
39
+ "fs-extra": "10.0.0"
39
40
  },
40
41
  "engines": {
41
42
  "node": ">=12.22.0 <=16.x.x",
42
43
  "npm": ">=6.0.0"
43
44
  },
44
- "gitHead": "c52f42c80df62e95876235db368f84a7eda09c97"
45
+ "gitHead": "1130511ce3b6762b3b3de972103d891c7bc69520"
45
46
  }