UNPKG

turbodepot-node

Version:

General purpose multi storage library (ORM, Logs, Users, Files, Objects)

1 lines 11.6 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.FilesManager=void 0;var turbocommons_ts_1=require("turbocommons-ts"),FilesManager=function(){function t(t){if(void 0===t&&(t=""),this._rootPath="",this.fs=require("fs"),this.os=require("os"),this.path=require("path"),this.crypto=require("crypto"),!turbocommons_ts_1.StringUtils.isString(t))throw new Error("rootPath must be a string");if(this._rootPath=turbocommons_ts_1.StringUtils.formatPath(t),""!==this._rootPath&&!this.isDirectory(this._rootPath))throw new Error("Specified rootPath does not exist: "+t)}return t.prototype.dirSep=function(){return this.path.sep},t.prototype.isPathAbsolute=function(t){if(turbocommons_ts_1.StringUtils.isString(t)){var r=t.length,e=r>0&&/^[a-zA-Z]+$/.test(t.charAt(0));return 0===t.indexOf("/")||0===t.indexOf("\\")||2===r&&e&&":"===t.charAt(1)||r>2&&e&&":"===t.charAt(1)&&(2===t.indexOf("/")||2===t.indexOf("\\"))}throw new Error("path must be a string")},t.prototype.isFile=function(t){if(!turbocommons_ts_1.StringUtils.isString(t))throw new Error("path must be a string");try{return this.fs.lstatSync(this._composePath(t)).isFile()}catch(t){return!1}},t.prototype.isFileEqualTo=function(t,r){return t=this._composePath(t,!1,!0),r=this._composePath(r,!1,!0),this.getFileSize(t)===this.getFileSize(r)&&this.crypto.createHash("md5").update(this.readFile(t),"utf8").digest("hex")===this.crypto.createHash("md5").update(this.readFile(r),"utf8").digest("hex")},t.prototype.isDirectory=function(t){if(!turbocommons_ts_1.StringUtils.isString(t))throw new Error("path must be a string");if(t=this._composePath(t),turbocommons_ts_1.StringUtils.isEmpty(t))return!1;try{return this.fs.lstatSync(this.fs.realpathSync(t)).isDirectory()}catch(t){return!1}},t.prototype.isDirectoryEqualTo=function(t,r){t=this._composePath(t),r=this._composePath(r);var e=this.getDirectoryList(t,"nameAsc"),i=this.getDirectoryList(r,"nameAsc");if(!turbocommons_ts_1.ArrayUtils.isEqualTo(e,i))return!1;for(var o=0;o<e.length;o++){var s=t+this.dirSep()+e[o],n=r+this.dirSep()+i[o],h=this.isDirectory(s);if(h&&!this.isDirectoryEqualTo(s,n))return!1;if(!h&&!this.isFileEqualTo(s,n))return!1}return!0},t.prototype.isDirectoryEmpty=function(t){return this.getDirectoryList(t).length<=0},t.prototype.countDirectoryItems=function(t,r,e,i,o){return void 0===r&&(r="both"),void 0===e&&(e=-1),void 0===i&&(i=/.*/),void 0===o&&(o=""),this.findDirectoryItems(t,i,"relative",r,e,o).length},t.prototype.findDirectoryItems=function(t,r,e,i,o,s,n){var h=this;void 0===e&&(e="relative"),void 0===i&&(i="both"),void 0===o&&(o=-1),void 0===s&&(s=""),void 0===n&&(n="name");for(var a=[],c=[[t=this._composePath(t,!0),0]],p=new RegExp(r),u=s?new RegExp(s):null;c.length>0;){var l=c.pop(),m=l[0],f=l[1];if(!(-1!==o&&f>o))for(var y=0,d=this.fs.readdirSync(m,{withFileTypes:!0});y<d.length;y++){var g=d[y],_=this.path.join(m,g.name),v="absolute"===n?_:g.name;null!==u&&void 0!==u&&u.test(_)||(g.isDirectory()?("files"!==i&&p.test(v)&&a.push(_),(-1===o||f<o)&&c.push([_,f+1])):g.isFile()&&"folders"!==i&&p.test(v)&&a.push(_))}}var S=[];if(0!==a.length)switch(e){case"relative":S=a.map(function(r){return h.path.relative(t,r)});break;case"name":S=a.map(function(t){return h.path.basename(t)});break;case"name-noext":S=a.map(function(t){return h.path.basename(t,h.path.extname(t))});break;case"absolute":S=a;break;default:throw new Error("Invalid returnFormat: "+e)}return S.sort(function(t,r){return t.localeCompare(r)})},t.prototype.findUniqueDirectoryName=function(t,r,e,i,o){if(void 0===r&&(r=""),void 0===e&&(e=""),void 0===i&&(i="-"),void 0===o&&(o=!1),!turbocommons_ts_1.StringUtils.isString(t))throw new Error("path must be a string");if(!turbocommons_ts_1.StringUtils.isString(r))throw new Error("desiredName must be a string");if(!turbocommons_ts_1.StringUtils.isString(e))throw new Error("text must be a string");if(!turbocommons_ts_1.StringUtils.isString(i))throw new Error("separator must be a string");t=this._composePath(t,!0);for(var s=1,n=turbocommons_ts_1.StringUtils.isEmpty(r)?String(s):r;this.isDirectory(t+this.dirSep()+n)||this.isFile(t+this.dirSep()+n);)n=this._generateUniqueNameAux(s,r,e,i,o),s++;return n},t.prototype.findUniqueFileName=function(t,r,e,i,o){void 0===r&&(r=""),void 0===e&&(e=""),void 0===i&&(i="-"),void 0===o&&(o=!1);var s=1,n=""==r?s:r;t=this._composePath(t,!0);var h=turbocommons_ts_1.StringUtils.getPathExtension(r);for(""!==h&&(h="."+h);this.isDirectory(t+this.dirSep()+n)||this.isFile(t+this.dirSep()+n);)n=this._generateUniqueNameAux(s,turbocommons_ts_1.StringUtils.getPathElementWithoutExt(r),e,i,o)+h,s++;return n},t.prototype.createDirectory=function(t,r){if(void 0===r&&(r=!1),!turbocommons_ts_1.StringUtils.isString(t)||turbocommons_ts_1.StringUtils.isEmpty(t))throw new Error("Path must be a non empty string");if(/[*"<>|?\r\n]/.test(t))throw new Error('Forbidden * " < > | ? chars found in path: '+t);if(t=this._composePath(t),this.isDirectory(t))return!1;if(this.isFile(t))throw new Error("specified path is an existing file "+t);try{r?this.fs.mkdirSync(t,{recursive:!0}):this.fs.mkdirSync(t)}catch(r){if(!this.isDirectory(t))throw new Error(r.message+" "+t);return!1}return!0},t.prototype.getOSTempDirectory=function(){return turbocommons_ts_1.StringUtils.formatPath(this.os.tmpdir(),this.dirSep())},t.prototype.createTempDirectory=function(r,e){var i=this;void 0===e&&(e=!0);var o=this.getOSTempDirectory(),s=o+this.dirSep()+this.findUniqueDirectoryName(o,r);if(!this.createDirectory(s))throw new Error("Could not create TMP directory "+s);return e&&(t._tempDirectoriesToDelete.push(s),t._tempDirectoriesToDelete.length<2&&process.once("exit",function(){for(var r=0,e=t._tempDirectoriesToDelete;r<e.length;r++){var o=e[r];i.isDirectory(o)&&i.deleteDirectory(o)}})),s},t.prototype.getDirectoryList=function(t,r){void 0===r&&(r=""),t=this._composePath(t,!0);for(var e=[],i=0,o=this.fs.readdirSync(t);i<o.length;i++){var s=o[i];if("."!==s&&".."!==s)switch(r){case"mDateAsc":case"mDateDesc":break;default:e.push(s)}}switch(r){case"nameAsc":e.sort();break;case"nameDesc":e.sort(),e.reverse();break;case"mDateAsc":case"mDateDesc":break;default:if(""!==r)throw new Error("Unknown sort method")}return e},t.prototype.getDirectorySize=function(t){t=this._composePath(t);for(var r=0,e=0,i=this.getDirectoryList(t);e<i.length;e++){var o=i[e],s=t+this.dirSep()+o;r+=this.isDirectory(s)?this.getDirectorySize(s):this.getFileSize(s)}return r},t.prototype.copyDirectory=function(t,r,e){if(void 0===e&&(e=!0),(t=this._composePath(t))===(r=this._composePath(r)))throw new Error("cannot copy a directory into itself: "+t);if(e&&!this.isDirectoryEmpty(r))throw new Error("destPath must be empty");for(var i=0,o=this.getDirectoryList(t);i<o.length;i++){var s=o[i],n=t+this.dirSep()+s,h=r+this.dirSep()+s;if(this.isDirectory(n)){if(!this.isDirectory(h)&&!this.createDirectory(h))return!1;if(!this.copyDirectory(n,h,e))return!1}else if(!this.copyFile(n,h))return!1}return!0},t.prototype.mirrorDirectory=function(t,r,e){if(void 0===e&&(e=15),(t=this._composePath(t,!0))===(r=this._composePath(r,!0)))throw new Error("cannot mirror a directory into itself: "+t);for(var i=this.getDirectoryList(t),o=0,s=i;o<s.length;o++){var n=s[o],h=t+this.dirSep()+n,a=r+this.dirSep()+n;if(this.isDirectory(h))this.isFile(a)&&this.deleteFile(a,e),this.isDirectory(a)?this.mirrorDirectory(h,a,e):(this.createDirectory(a),this.copyDirectory(h,a,!0));else if(this.isDirectory(a)&&this.deleteDirectory(a,!0,e),!(this.isFile(a)&&this.isFileEqualTo(h,a)||this.copyFile(h,a)))throw new Error("Could not copy file from source <"+h+"> to destination <"+a+">")}for(var c=0,p=this.getDirectoryList(r).filter(function(t){return i.indexOf(t)<0});c<p.length;c++){var u=p[c];a=r+this.dirSep()+u;this.isDirectory(a)?this.deleteDirectory(a,!0,e):this.deleteFile(a,e)}return!0},t.prototype.syncDirectories=function(){},t.prototype.renameDirectory=function(t,r,e){return void 0===e&&(e=15),this._renameFSResource(this._composePath(t,!0),this._composePath(r),e)},t.prototype._renameFSResource=function(t,r,e){if(this.isDirectory(r)||this.isFile(r))throw new Error("Invalid destination: "+r);if(this.path.resolve(turbocommons_ts_1.StringUtils.getPath(t))!==this.path.resolve(turbocommons_ts_1.StringUtils.getPath(r)))throw new Error("Source and dest must be on the same path");var i="",o=0,s=Math.floor(Date.now()/1e3);do{try{return this.fs.renameSync(t,r),!0}catch(t){i=t.toString()}o=Math.floor(Date.now()/1e3)-s}while(o<e);throw new Error("Error renaming ("+o+" seconds timeout):\n"+t+"\n"+i)},t.prototype.deleteDirectory=function(t,r,e){void 0===r&&(r=!0),void 0===e&&(e=15);var i=0;t=this._composePath(t,!0);for(var o=0,s=this.getDirectoryList(t);o<s.length;o++){var n=s[o];this.isDirectory(t+this.dirSep()+n)?i+=this.deleteDirectory(t+this.dirSep()+n,!0,e):(this.deleteFile(t+this.dirSep()+n,e),i++)}if(r){var h="",a=0,c=Math.floor(Date.now()/1e3);do{try{return this.fs.rmdirSync(t),i}catch(t){h=t.toString()}a=Math.floor(Date.now()/1e3)-c}while(a<e);throw new Error("Could not delete directory itself ("+a+" seconds timeout):\n"+t+"\n"+h)}return i},t.prototype.saveFile=function(t,r,e,i){void 0===r&&(r=""),void 0===e&&(e=!1),void 0===i&&(i=!1),t=this._composePath(t),i&&this.createDirectory(turbocommons_ts_1.StringUtils.getPath(t),!0);try{return e?this.fs.appendFileSync(t,r):this.fs.writeFileSync(t,r),!0}catch(r){throw new Error("Could not write to file: "+t)}},t.prototype.createTempFile=function(){},t.prototype.mergeFiles=function(t,r,e){void 0===e&&(e="");for(var i="",o=0;o<t.length;o++)i+=this.readFile(this._composePath(t[o])),o<t.length-1&&""!==e&&(i+=e);return this.saveFile(r,i)},t.prototype.getFileSize=function(t){t=this._composePath(t,!1,!0);try{return this.fs.statSync(t).size}catch(t){throw new Error("Error reading file size")}},t.prototype.getFileModificationTime=function(t){return t},t.prototype.readFile=function(t){return t=this._composePath(t,!1,!0),this.fs.readFileSync(t,"utf8")},t.prototype.readFileAsBase64=function(t){return t=this._composePath(t,!1,!0),this.fs.readFileSync(t,{encoding:"base64"})},t.prototype.readFileBuffered=function(){},t.prototype.copyFile=function(t,r){t=this._composePath(t),r=this._composePath(r);try{return this.fs.copyFileSync(t,r),!0}catch(t){return!1}},t.prototype.renameFile=function(t,r,e){return void 0===e&&(e=15),this._renameFSResource(this._composePath(t,!1,!0),this._composePath(r),e)},t.prototype.deleteFile=function(t,r){void 0===r&&(r=15),t=this._composePath(t,!1,!0);var e="",i=0,o=Date.now();if(this.fs.existsSync(t))do{try{return this.fs.unlinkSync(t),!0}catch(t){e=t.toString()}i=Date.now()-o}while(i<1e3*r);else e="File does not exist";throw new Error("Error deleting file ("+i+" seconds timeout):\n"+t+"\n"+e)},t.prototype.deleteFiles=function(t,r){void 0===r&&(r=15);for(var e=0,i=t;e<i.length;e++){var o=i[e];this.deleteFile(o,r)}return!0},t.prototype._generateUniqueNameAux=function(t,r,e,i,o){var s=[];return o?(""!==e&&s.push(e),s.push(String(t)),""!==r&&s.push(r)):(""!==r&&s.push(r),""!==e&&s.push(e),s.push(String(t))),s.join(i)},t.prototype._composePath=function(t,r,e){if(void 0===r&&(r=!1),void 0===e&&(e=!1),!turbocommons_ts_1.StringUtils.isString(t))throw new Error("Path must be a string");var i="";i=turbocommons_ts_1.StringUtils.isEmpty(this._rootPath)||this.isPathAbsolute(t)?t:this._rootPath+this.dirSep()+t;var o=turbocommons_ts_1.StringUtils.formatPath(i,this.dirSep());if(r&&!this.isDirectory(o))throw new Error("Path does not exist: "+o);if(e&&!this.isFile(o))throw new Error("File does not exist: "+o);return o},t._tempDirectoriesToDelete=[],t}();exports.FilesManager=FilesManager;