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