@cyberskill/shared 2.8.1 → 2.9.0
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});exports.E_UploadType=(
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});exports.E_UploadType=(O=>(O.IMAGE="IMAGE",O.VIDEO="VIDEO",O.AUDIO="AUDIO",O.DOCUMENT="DOCUMENT",O.OTHER="OTHER",O))(exports.E_UploadType||{});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export declare enum E_UploadType {
|
|
2
2
|
IMAGE = "IMAGE",
|
|
3
3
|
VIDEO = "VIDEO",
|
|
4
|
+
AUDIO = "AUDIO",
|
|
4
5
|
DOCUMENT = "DOCUMENT",
|
|
5
6
|
OTHER = "OTHER"
|
|
6
7
|
}
|
|
@@ -15,6 +16,7 @@ export interface I_UploadTypeConfig {
|
|
|
15
16
|
export interface I_UploadConfig {
|
|
16
17
|
[E_UploadType.IMAGE]: I_UploadTypeConfig;
|
|
17
18
|
[E_UploadType.VIDEO]: I_UploadTypeConfig;
|
|
19
|
+
[E_UploadType.AUDIO]: I_UploadTypeConfig;
|
|
18
20
|
[E_UploadType.DOCUMENT]: I_UploadTypeConfig;
|
|
19
21
|
[E_UploadType.OTHER]: I_UploadTypeConfig;
|
|
20
22
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var D = /* @__PURE__ */ ((O) => (O.IMAGE = "IMAGE", O.VIDEO = "VIDEO", O.AUDIO = "AUDIO", O.DOCUMENT = "DOCUMENT", O.OTHER = "OTHER", O))(D || {});
|
|
2
2
|
export {
|
|
3
|
-
|
|
3
|
+
D as E_UploadType
|
|
4
4
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const z=require("node:buffer"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const z=require("node:buffer"),x=require("node:stream"),C=require("node:stream/web"),A=require("./upload.constant.cjs"),d=require("./upload.type.cjs"),D=require("../path/path.util.cjs"),S=require("../fs/fs.util.cjs"),f=require("../../constant/response-status.cjs");var P=Object.defineProperty,v=Object.getOwnPropertySymbols,h=Object.prototype.hasOwnProperty,F=Object.prototype.propertyIsEnumerable,T=(i,t,e)=>t in i?P(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,w=(i,t)=>{for(var e in t||(t={}))h.call(t,e)&&T(i,e,t[e]);if(v)for(var e of v(t))F.call(t,e)&&T(i,e,t[e]);return i},m=(i,t,e)=>new Promise((r,a)=>{var s=l=>{try{n(e.next(l))}catch(c){a(c)}},o=l=>{try{n(e.throw(l))}catch(c){a(c)}},n=l=>l.done?r(l.value):Promise.resolve(l.value).then(s,o);n((e=e.apply(i,t)).next())});function O(i){return m(this,null,function*(){return new Promise((t,e)=>{let r=0;i.on("data",a=>{r+=a.length}),i.on("end",()=>t(r)),i.on("error",e)})})}function _(i,t,e){return m(this,null,function*(){const r=yield(yield t).file,a=r.createReadStream(),s=yield O(a),o=e!=null?e:y(),n=R({filename:r.filename,fileSize:s},o,i);return n.isValid?{success:!0,result:r,message:"File validated successfully"}:{success:!1,message:n.error||"File validation failed",code:f.RESPONSE_STATUS.BAD_REQUEST.CODE}})}function L(i,t,e){return m(this,null,function*(){const a=(e!=null?e:y())[i],s=yield _(i,t,e);if(!s.success)return s;const{createReadStream:o}=s.result;let n=a.sizeLimit;const l=new x.Transform({transform(u,p,g){n-=u.length,n<0?g(new Error(`File size exceeds limit of ${a.sizeLimit/A.BYTES_PER_MB}MB`)):g(null,u)}}),E=o().pipe(l);return{success:!0,result:new C.ReadableStream({start(u){E.on("data",p=>{u.enqueue(typeof p=="string"?z.Buffer.from(p):p)}),E.on("end",()=>u.close()),E.on("error",p=>u.error(p))}})}})}function U(i,t){const e=i.lastIndexOf(".");if(e===-1)return!1;const r=i.substring(e+1).toLowerCase();return t.includes(r)}function R(i,t,e){const{filename:r,fileSize:a}=i,s=t[e],{allowedExtensions:o,sizeLimit:n}=s;if(!U(r,o))return{isValid:!1,error:`File extension not allowed for ${e.toLowerCase()} files. Allowed extensions: ${o.join(", ")}`};if(a!==void 0&&a>n){const l=Math.round(n/1048576);return{isValid:!1,error:`File size exceeds limit for ${e.toLowerCase()} files. Maximum size: ${l}MB`}}return{isValid:!0}}function y(i){const t={[d.E_UploadType.IMAGE]:{allowedExtensions:["jpg","jpeg","png","gif","webp","svg"],sizeLimit:5242880},[d.E_UploadType.VIDEO]:{allowedExtensions:["mp4","avi","mov","wmv","flv","webm"],sizeLimit:524288e3},[d.E_UploadType.AUDIO]:{allowedExtensions:["mp3","wav","ogg","m4a","aac"],sizeLimit:52428800},[d.E_UploadType.DOCUMENT]:{allowedExtensions:["pdf","doc","docx","txt","rtf"],sizeLimit:10485760},[d.E_UploadType.OTHER]:{allowedExtensions:["zip","rar","tar","gz"],sizeLimit:5242880}};return w(w({},t),i)}function I(i){return m(this,null,function*(){const{path:t,file:e,config:r,type:a}=i;if(!t||typeof t!="string")return{success:!1,message:"Invalid path provided",code:f.RESPONSE_STATUS.BAD_REQUEST.CODE};if(!e||typeof e!="object")return{success:!1,message:"Invalid file provided",code:f.RESPONSE_STATUS.BAD_REQUEST.CODE};if(r){const s=[d.E_UploadType.IMAGE,d.E_UploadType.VIDEO,d.E_UploadType.DOCUMENT,d.E_UploadType.OTHER];for(const o of s){if(!r[o]||!Array.isArray(r[o].allowedExtensions)||r[o].allowedExtensions.length===0)return{success:!1,message:`Invalid config for ${o.toLowerCase()} files`,code:f.RESPONSE_STATUS.BAD_REQUEST.CODE};if(typeof r[o].sizeLimit!="number"||r[o].sizeLimit<=0)return{success:!1,message:`Invalid size limit for ${o.toLowerCase()} files`,code:f.RESPONSE_STATUS.BAD_REQUEST.CODE}}}try{const s=yield _(a,yield e,r);if(!s.success)return s;const{createReadStream:o}=s.result,n=D.dirname(t);S.pathExistsSync(n)||S.mkdirSync(n,{recursive:!0});const l=o(),c=S.createWriteStream(t);return l.pipe(c),yield new Promise((E,u)=>{c.on("finish",()=>E()),c.on("error",u),l.on("error",u)}),{success:!0,result:t,message:"File uploaded successfully",code:f.RESPONSE_STATUS.OK.CODE}}catch(s){return{success:!1,message:s instanceof Error?s.message:"File upload failed",code:f.RESPONSE_STATUS.INTERNAL_SERVER_ERROR.CODE}}})}exports.createUploadConfig=y;exports.getAndValidateFile=_;exports.getFileSizeFromStream=O;exports.getFileWebStream=L;exports.upload=I;exports.validateFileExtension=U;exports.validateUpload=R;
|
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import { Buffer as
|
|
1
|
+
import { Buffer as _ } from "node:buffer";
|
|
2
2
|
import { Transform as x } from "node:stream";
|
|
3
3
|
import { ReadableStream as z } from "node:stream/web";
|
|
4
4
|
import { BYTES_PER_MB as C } from "./upload.constant.js";
|
|
5
|
-
import { E_UploadType as
|
|
5
|
+
import { E_UploadType as c } from "./upload.type.js";
|
|
6
6
|
import { dirname as D } from "../path/path.util.js";
|
|
7
7
|
import { pathExistsSync as R, mkdirSync as h, createWriteStream as L } from "../fs/fs.util.js";
|
|
8
8
|
import { RESPONSE_STATUS as d } from "../../constant/response-status.js";
|
|
9
|
-
var T = Object.defineProperty, v = Object.getOwnPropertySymbols, A = Object.prototype.hasOwnProperty, I = Object.prototype.propertyIsEnumerable,
|
|
10
|
-
for (var e in
|
|
11
|
-
A.call(
|
|
9
|
+
var T = Object.defineProperty, v = Object.getOwnPropertySymbols, A = Object.prototype.hasOwnProperty, I = Object.prototype.propertyIsEnumerable, w = (r, t, e) => t in r ? T(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e, y = (r, t) => {
|
|
10
|
+
for (var e in t || (t = {}))
|
|
11
|
+
A.call(t, e) && w(r, e, t[e]);
|
|
12
12
|
if (v)
|
|
13
|
-
for (var e of v(
|
|
14
|
-
I.call(
|
|
15
|
-
return
|
|
16
|
-
}, E = (
|
|
13
|
+
for (var e of v(t))
|
|
14
|
+
I.call(t, e) && w(r, e, t[e]);
|
|
15
|
+
return r;
|
|
16
|
+
}, E = (r, t, e) => new Promise((i, a) => {
|
|
17
17
|
var s = (l) => {
|
|
18
18
|
try {
|
|
19
19
|
n(e.next(l));
|
|
20
|
-
} catch (
|
|
21
|
-
a(
|
|
20
|
+
} catch (u) {
|
|
21
|
+
a(u);
|
|
22
22
|
}
|
|
23
23
|
}, o = (l) => {
|
|
24
24
|
try {
|
|
25
25
|
n(e.throw(l));
|
|
26
|
-
} catch (
|
|
27
|
-
a(
|
|
26
|
+
} catch (u) {
|
|
27
|
+
a(u);
|
|
28
28
|
}
|
|
29
29
|
}, n = (l) => l.done ? i(l.value) : Promise.resolve(l.value).then(s, o);
|
|
30
|
-
n((e = e.apply(
|
|
30
|
+
n((e = e.apply(r, t)).next());
|
|
31
31
|
});
|
|
32
|
-
function P(
|
|
32
|
+
function P(r) {
|
|
33
33
|
return E(this, null, function* () {
|
|
34
|
-
return new Promise((
|
|
34
|
+
return new Promise((t, e) => {
|
|
35
35
|
let i = 0;
|
|
36
|
-
|
|
36
|
+
r.on("data", (a) => {
|
|
37
37
|
i += a.length;
|
|
38
|
-
}),
|
|
38
|
+
}), r.on("end", () => t(i)), r.on("error", e);
|
|
39
39
|
});
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
|
-
function
|
|
42
|
+
function S(r, t, e) {
|
|
43
43
|
return E(this, null, function* () {
|
|
44
|
-
const i = yield (yield
|
|
44
|
+
const i = yield (yield t).file, a = i.createReadStream(), s = yield P(a), o = e != null ? e : O(), n = F(
|
|
45
45
|
{ filename: i.filename, fileSize: s },
|
|
46
46
|
o,
|
|
47
|
-
|
|
47
|
+
r
|
|
48
48
|
);
|
|
49
49
|
return n.isValid ? {
|
|
50
50
|
success: !0,
|
|
@@ -57,9 +57,9 @@ function w(t, r, e) {
|
|
|
57
57
|
};
|
|
58
58
|
});
|
|
59
59
|
}
|
|
60
|
-
function q(
|
|
60
|
+
function q(r, t, e) {
|
|
61
61
|
return E(this, null, function* () {
|
|
62
|
-
const a = (e != null ? e :
|
|
62
|
+
const a = (e != null ? e : O())[r], s = yield S(r, t, e);
|
|
63
63
|
if (!s.success)
|
|
64
64
|
return s;
|
|
65
65
|
const { createReadStream: o } = s.result;
|
|
@@ -74,22 +74,22 @@ function q(t, r, e) {
|
|
|
74
74
|
result: new z({
|
|
75
75
|
start(f) {
|
|
76
76
|
p.on("data", (m) => {
|
|
77
|
-
f.enqueue(typeof m == "string" ?
|
|
77
|
+
f.enqueue(typeof m == "string" ? _.from(m) : m);
|
|
78
78
|
}), p.on("end", () => f.close()), p.on("error", (m) => f.error(m));
|
|
79
79
|
}
|
|
80
80
|
})
|
|
81
81
|
};
|
|
82
82
|
});
|
|
83
83
|
}
|
|
84
|
-
function B(
|
|
85
|
-
const e =
|
|
84
|
+
function B(r, t) {
|
|
85
|
+
const e = r.lastIndexOf(".");
|
|
86
86
|
if (e === -1)
|
|
87
87
|
return !1;
|
|
88
|
-
const i =
|
|
89
|
-
return
|
|
88
|
+
const i = r.substring(e + 1).toLowerCase();
|
|
89
|
+
return t.includes(i);
|
|
90
90
|
}
|
|
91
|
-
function F(
|
|
92
|
-
const { filename: i, fileSize: a } =
|
|
91
|
+
function F(r, t, e) {
|
|
92
|
+
const { filename: i, fileSize: a } = r, s = t[e], { allowedExtensions: o, sizeLimit: n } = s;
|
|
93
93
|
if (!B(i, o))
|
|
94
94
|
return {
|
|
95
95
|
isValid: !1,
|
|
@@ -104,35 +104,40 @@ function F(t, r, e) {
|
|
|
104
104
|
}
|
|
105
105
|
return { isValid: !0 };
|
|
106
106
|
}
|
|
107
|
-
function
|
|
108
|
-
const
|
|
109
|
-
[
|
|
107
|
+
function O(r) {
|
|
108
|
+
const t = {
|
|
109
|
+
[c.IMAGE]: {
|
|
110
110
|
allowedExtensions: ["jpg", "jpeg", "png", "gif", "webp", "svg"],
|
|
111
111
|
sizeLimit: 5242880
|
|
112
112
|
// 5MB
|
|
113
113
|
},
|
|
114
|
-
[
|
|
114
|
+
[c.VIDEO]: {
|
|
115
115
|
allowedExtensions: ["mp4", "avi", "mov", "wmv", "flv", "webm"],
|
|
116
116
|
sizeLimit: 524288e3
|
|
117
117
|
// 500MB
|
|
118
118
|
},
|
|
119
|
-
[
|
|
119
|
+
[c.AUDIO]: {
|
|
120
|
+
allowedExtensions: ["mp3", "wav", "ogg", "m4a", "aac"],
|
|
121
|
+
sizeLimit: 52428800
|
|
122
|
+
// 50MB
|
|
123
|
+
},
|
|
124
|
+
[c.DOCUMENT]: {
|
|
120
125
|
allowedExtensions: ["pdf", "doc", "docx", "txt", "rtf"],
|
|
121
126
|
sizeLimit: 10485760
|
|
122
127
|
// 10MB
|
|
123
128
|
},
|
|
124
|
-
[
|
|
129
|
+
[c.OTHER]: {
|
|
125
130
|
allowedExtensions: ["zip", "rar", "tar", "gz"],
|
|
126
131
|
sizeLimit: 5242880
|
|
127
132
|
// 5MB
|
|
128
133
|
}
|
|
129
134
|
};
|
|
130
|
-
return
|
|
135
|
+
return y(y({}, t), r);
|
|
131
136
|
}
|
|
132
|
-
function G(
|
|
137
|
+
function G(r) {
|
|
133
138
|
return E(this, null, function* () {
|
|
134
|
-
const { path:
|
|
135
|
-
if (!
|
|
139
|
+
const { path: t, file: e, config: i, type: a } = r;
|
|
140
|
+
if (!t || typeof t != "string")
|
|
136
141
|
return {
|
|
137
142
|
success: !1,
|
|
138
143
|
message: "Invalid path provided",
|
|
@@ -145,7 +150,7 @@ function G(t) {
|
|
|
145
150
|
code: d.BAD_REQUEST.CODE
|
|
146
151
|
};
|
|
147
152
|
if (i) {
|
|
148
|
-
const s = [
|
|
153
|
+
const s = [c.IMAGE, c.VIDEO, c.DOCUMENT, c.OTHER];
|
|
149
154
|
for (const o of s) {
|
|
150
155
|
if (!i[o] || !Array.isArray(i[o].allowedExtensions) || i[o].allowedExtensions.length === 0)
|
|
151
156
|
return {
|
|
@@ -162,17 +167,17 @@ function G(t) {
|
|
|
162
167
|
}
|
|
163
168
|
}
|
|
164
169
|
try {
|
|
165
|
-
const s = yield
|
|
170
|
+
const s = yield S(a, yield e, i);
|
|
166
171
|
if (!s.success)
|
|
167
172
|
return s;
|
|
168
|
-
const { createReadStream: o } = s.result, n = D(
|
|
173
|
+
const { createReadStream: o } = s.result, n = D(t);
|
|
169
174
|
R(n) || h(n, { recursive: !0 });
|
|
170
|
-
const l = o(),
|
|
171
|
-
return l.pipe(
|
|
172
|
-
|
|
175
|
+
const l = o(), u = L(t);
|
|
176
|
+
return l.pipe(u), yield new Promise((p, f) => {
|
|
177
|
+
u.on("finish", () => p()), u.on("error", f), l.on("error", f);
|
|
173
178
|
}), {
|
|
174
179
|
success: !0,
|
|
175
|
-
result:
|
|
180
|
+
result: t,
|
|
176
181
|
message: "File uploaded successfully",
|
|
177
182
|
code: d.OK.CODE
|
|
178
183
|
};
|
|
@@ -186,8 +191,8 @@ function G(t) {
|
|
|
186
191
|
});
|
|
187
192
|
}
|
|
188
193
|
export {
|
|
189
|
-
|
|
190
|
-
|
|
194
|
+
O as createUploadConfig,
|
|
195
|
+
S as getAndValidateFile,
|
|
191
196
|
P as getFileSizeFromStream,
|
|
192
197
|
q as getFileWebStream,
|
|
193
198
|
G as upload,
|