text: "use strict";var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var t,r=1,s=arguments.length;r<s;r++)for(var n in t=arguments[r])Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e}).apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,i,a,u){return new(a=a||Promise)(function(r,t){function s(e){try{o(u.next(e))}catch(e){t(e)}}function n(e){try{o(u.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?r(e.value):((t=e.value)instanceof a?t:new a(function(e){e(t)})).then(s,n)}o((u=u.apply(e,i||[])).next())})},__generator=this&&this.__generator||function(r,s){var n,o,i,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},e={next:t(0),throw:t(1),return:t(2)};return"function"==typeof Symbol&&(e[Symbol.iterator]=function(){return this}),e;function t(t){return function(e){return function(t){if(n)throw new TypeError("Generator is already executing.");for(;a;)try{if(n=1,o&&(i=2&t[0]?o.return:t[0]?o.throw||((i=o.return)&&i.call(o),0):o.next)&&!(i=i.call(o,t[1])).done)return i;switch(o=0,(t=i?[2&t[0],i.value]:t)[0]){case 0:case 1:i=t;break;case 4:return a.label++,{value:t[1],done:!1};case 5:a.label++,o=t[1],t=[0];continue;case 7:t=a.ops.pop(),a.trys.pop();continue;default:if(!(i=0<(i=a.trys).length&&i[i.length-1])&&(6===t[0]||2===t[0])){a=0;continue}if(3===t[0]&&(!i||t[1]>i[0]&&t[1]<i[3])){a.label=t[1];break}if(6===t[0]&&a.label<i[1]){a.label=i[1],i=t;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(t);break}i[2]&&a.ops.pop(),a.trys.pop();continue}t=s.call(r,a)}catch(e){t=[6,e],o=0}finally{n=i=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}([t,e])}}},__spreadArray=this&&this.__spreadArray||function(e,t){for(var r=0,s=t.length,n=e.length;r<s;r++,n++)e[n]=t[r];return e};Object.defineProperty(exports,"__esModule",{value:!0}),exports.buildItem=exports.removeResources=exports.resources=exports.updateResources=exports.addResources=exports.removeItem=exports.searchItem=exports.items=exports.getItemData=exports.updateAppItem=exports.addItem=void 0;var fs=require("fs-extra"),fetch=require("cross-fetch");require("../../../global");var utils_1=require("./utils");require("isomorphic-form-data"),global.fetch=fetch;var url=require("url"),uploadThumbnail=utils_1.getThumbnailUpdateMulter(),upload=utils_1.getUploadMulter();function addItem(f,_){return __awaiter(this,void 0,void 0,function(){var t,r,s,n,o,i,a,u,l,c,d,p;return __generator(this,function(e){switch(e.label){case 0:t={error:{message:"",success:!1}};try{if(f.type="json",(r=f.request.body||{}).typeKeywords?r.typeKeywords=Array.isArray(r.typeKeywords)?r.typeKeywords:r.typeKeywords.split(","):delete r.typeKeywords,r.hasOwnProperty("snippet")&&delete r.snippet,delete r.thumbnail,s=utils_1.addItemParamsIsPass(r))return t.error.message=s,[2,utils_1.requestException(t,f)];fs.ensureDirSync(utils_1.appFolderPath),n=Date.now(),o=fs.readdirSync(utils_1.appFolderPath),i=utils_1.getFolderIndex(o,0)+"",r.id=i,r.created=n,r.modified=n,r.owner=r.username,delete(a=__assign(__assign({},utils_1.infoJson),r)).text,a=utils_1.deepClone(a),u=utils_1.appFolderPath+"/"+i,fs.mkdirSync(u),l=JSON.stringify(a,null,2),fs.writeFileSync(u+"/info.json",l),c={__not_publish:!0},d=JSON.stringify(c),r.text&&(d="string"==typeof r.text?r.text:JSON.stringify(r.text,null,2)),fs.writeFileSync(u+"/config.json",d),p={folder:"apps/"+i,id:i,success:!0},utils_1.commonResponse(f,p)}catch(e){t.error.message=e,utils_1.writeResponseLog(e,!0),f.body=t}return[4,_()];case 1:return e.sent(),[2]}})})}function updateAppItem(l,r){return __awaiter(this,void 0,void 0,function(){var u,t;return __generator(this,function(e){switch(e.label){case 0:u={message:"",itemId:"",success:!1},e.label=1;case 1:return e.trys.push([1,3,,4]),[4,uploadThumbnail.single("thumbnail")(l,r).then(function(e){var t=l.request.body||{},r=utils_1.getIdFromUrl(l),s=l.request.headers["content-type"];delete t.thumbnail,t.typeKeywords?t.typeKeywords=Array.isArray(t.typeKeywords)?t.typeKeywords:t.typeKeywords.split(","):delete t.typeKeywords;var n=utils_1.appFolderPath+"/"+r+"/info.json",o=utils_1.appFolderPath+"/"+r+"/config.json";if(!fs.existsSync(n)||!fs.existsSync(o))return u.message="no file",u.itemId=r,utils_1.requestException(u,l);var i,a=JSON.parse(fs.readFileSync(n,"utf8"));-1!=s.indexOf("/form-data")&&(i=fs.readdirSync(utils_1.appFolderPath+"/"+r+"/thumbnail"),a.thumbnail="thumbnail/"+i[0]),t.text&&(s="string"==typeof t.text?JSON.parse(t.text):t.text,i=JSON.stringify(__assign({},s),null,2),(null==s?void 0:s.__not_publish)||fs.writeFileSync(o,i),delete t.text),t.username&&(t.owner=t.username),t.modified=Date.now();t=JSON.stringify(__assign(__assign({},a),t),null,2);fs.writeFileSync(n,t),utils_1.commonResponse(l,{id:r,success:!0})}).catch(function(e){u.message="upload failed",l.body=u})];case 2:return e.sent(),[3,4];case 3:return t=e.sent(),u.message=t,utils_1.writeResponseLog(t,!0),l.body=u,[3,4];case 4:return[2]}})})}function getItemData(a,u){return __awaiter(this,void 0,void 0,function(){var t,r,s,n,o,i;return __generator(this,function(e){switch(e.label){case 0:t={error:{message:"",id:"",success:!1}};try{if(r=utils_1.getIdFromUrl(a),s=utils_1.appFolderPath+"/"+r+"/config.json",!fs.existsSync(s))return t.error.message="no file",t.error.id=r,n={message:"no item",id:r,success:!1},utils_1.commonResponse(a,n),[2,!1];o=JSON.parse(fs.readFileSync(s,"utf8")),i=__assign(__assign({},o),{id:r,success:!0}),utils_1.commonResponse(a,i)}catch(e){t.error.message=e,utils_1.writeResponseLog(e,!0),a.body=t}return[4,u()];case 1:return e.sent(),[2]}})})}function items(a,u){return __awaiter(this,void 0,void 0,function(){var t,r,s,n,o,i;return __generator(this,function(e){switch(e.label){case 0:if(t={error:{message:"",id:"",success:!1}},r=a.req.url,!(-1!=r.indexOf("/sharing/rest/content/users/items")&&7==r.split("/").length))return[2,!1];try{if(s=utils_1.getIdFromUrl(a,0),n=utils_1.appFolderPath+"/"+s+"/info.json",!fs.existsSync(n))return t.error.message="Item does not exist or is inaccessible.",t.error.id=s,[2,utils_1.requestException(t,a)];o=JSON.parse(fs.readFileSync(n,"utf8")),i=__assign(__assign({},o),{id:s,success:!0}),utils_1.commonResponse(a,i)}catch(e){t.error.message=e,utils_1.writeResponseLog(e,!0),a.body=t}return[4,u()];case 1:return e.sent(),[2]}})})}function searchItem(y,g){return __awaiter(this,void 0,void 0,function(){var t,r,s,n,o,i,a,u,l,c,d,p,f,_,m,h;return __generator(this,function(e){switch(e.label){case 0:try{t=[],r=void 0,s=y.request.method,r="GET"==s?url.parse(y.request.url,!0).query:y.request.body||{},n=utils_1.appFolderPath,fs.existsSync(n)&&(o=fs.readdirSync(n),i=r.q,a="",i&&-1!=i.indexOf("(")&&(a=i?i.split("(")[1].split(")")[0]:""),i&&-1!=i.indexOf("title:")&&(a=i?i.split("title:")[1]:""),u=[],o.forEach(function(e){e=n+"/"+e+"/info.json";fs.pathExistsSync(e)&&(e=JSON.parse(fs.readFileSync(e,"utf-8")),u.push(e))}),t=utils_1.fuzzyMatching(u,a,"title")),t=utils_1.filterByType(t,r.q),l=t,c=utils_1.getOwner(r.q),l=utils_1.filterDataByOwner(l,c.owner,c.isNot),(d=null==r?void 0:r.portalUrl)&&(l=utils_1.filterDataByPortalUrl(l,d)),r.sortOrder&&(l=utils_1.sortByNumber(l,"created",r.sortOrder)),"modified"==r.sortField&&(l=utils_1.sortByNumber(l,r.sortField)),"title"==r.sortField&&(l=utils_1.sortByInitial(l,r.sortField)),p=Number(r.start)||0,f=Number(r.num)||10,_=p+f,m=[],l.forEach(function(e,t){t+=1;p<=t&&t<_&&m.push(e)}),h={nextStart:_,num:m.length,query:r.q,results:m,start:r.start,total:l.length},utils_1.commonResponse(y,h)}catch(e){utils_1.writeResponseLog(e,!0),y.body=e}return[4,g()];case 1:return e.sent(),[2]}})})}function removeItem(o,i){return __awaiter(this,void 0,void 0,function(){var t,r,s,n;return __generator(this,function(e){switch(e.label){case 0:t={message:"",itemId:"",success:!1};try{if(r=utils_1.getIdFromUrl(o),s=utils_1.appFolderPath+"/"+r,!fs.existsSync(s))return t.message="no folder",t.itemId=r,[2,utils_1.requestException(t,o)];fs.removeSync(s),n={id:r,success:!0},utils_1.commonResponse(o,n)}catch(e){t.message=e,utils_1.writeResponseLog(e,!0),o.body=t}return[4,i()];case 1:return e.sent(),[2]}})})}function addResources(n,s){return __awaiter(this,void 0,void 0,function(){var t,r;return __generator(this,function(e){switch(e.label){case 0:t={error:{message:"",details:[],success:!1}},e.label=1;case 1:return e.trys.push([1,3,,4]),[4,upload.single("file")(n,s).then(function(e){var t=utils_1.getIdFromUrl(n),r=n.request.body,s=utils_1.appFolderPath+"/"+t+"/resources/"+r.resourcesPrefix+"/"+r.fileName;utils_1.formatJson(s,r.fileName);r=utils_1.getOwnerFromInfo(t);utils_1.commonResponse(n,{folder:t,itemId:t,owner:r,success:!0})}).catch(function(e){t.error.message="upload failed",n.body=t})];case 2:return e.sent(),[3,4];case 3:return r=e.sent(),t.error.message=r,utils_1.writeResponseLog(r,!0),n.body=t,[3,4];case 4:return[2]}})})}function updateResources(o,s){return __awaiter(this,void 0,void 0,function(){var t,r;return __generator(this,function(e){switch(e.label){case 0:t={message:"",success:!1},e.label=1;case 1:return e.trys.push([1,3,,4]),[4,upload.single("file")(o,s).then(function(e){var t=utils_1.getIdFromUrl(o),r=o.request.body,s=utils_1.appFolderPath+"/"+t+"/resources/"+r.resourcesPrefix+"/"+r.fileName,n=utils_1.getOwnerFromInfo(t);utils_1.formatJson(s,r.fileName),utils_1.commonResponse(o,{folder:t,itemId:t,owner:n,success:!0})}).catch(function(e){t.message="upload failed",o.body=t})];case 2:return e.sent(),[3,4];case 3:return r=e.sent(),t.message=r,utils_1.writeResponseLog(r,!0),o.body=t,[3,4];case 4:return[2]}})})}function resources(i,a){return __awaiter(this,void 0,void 0,function(){var t,r,s,n,o;return __generator(this,function(e){switch(e.label){case 0:try{if(t=utils_1.getIdFromUrl(i),r=utils_1.appFolderPath+"/"+t+"/resources",!fs.existsSync(r))return s={total:0,start:1,num:0,nextStart:-1,resources:[],success:!1},utils_1.commonResponse(i,s),[2,!1];n=utils_1.readFolder(r,"resources/"),o={total:n.length,start:1,num:n.length,nextStart:-1,resources:n,success:!0},utils_1.commonResponse(i,o)}catch(e){utils_1.writeResponseLog(e,!0),i.body=e}return[4,a()];case 1:return e.sent(),[2]}})})}function removeResources(i,a){return __awaiter(this,void 0,void 0,function(){var t,r,s,n,o;return __generator(this,function(e){switch(e.label){case 0:t={error:{code:404,message:"",details:[],id:"",success:!1}};try{if((r=i.request.body||{}).deleteAll=!!r.hasOwnProperty("deleteAll")&&r.deleteAll,s=utils_1.getIdFromUrl(i),t.error.id=s,!r.deleteAll&&!r.resource)return t.error.message="no resource",[2,utils_1.requestException(t,i)];if(n=r.deleteAll?utils_1.appFolderPath+"/"+s+"/resources":utils_1.appFolderPath+"/"+s+"/resources/"+r.resource,!fs.existsSync(n))return t.error.message="no folder",[2,utils_1.requestException(t,i)];fs.removeSync(n),o={id:s,success:!0},utils_1.commonResponse(i,o)}catch(e){t.error.message=e,utils_1.writeResponseLog(e,!0),i.body=t}return[4,a()];case 1:return e.sent(),[2]}})})}function buildItem(o,i){return __awaiter(this,void 0,void 0,function(){var t,r,s,a,u,n;return __generator(this,function(e){switch(e.label){case 0:t={error:{message:"",itemId:"",detail:Object,success:!1}},e.label=1;case 1:return e.trys.push([1,3,,4]),r=require("child_process"),s=require("path"),a=utils_1.getIdFromUrl(o),u=function(e,t){for(var r=[],s=2;s<arguments.length;s++)r[s-2]=arguments[s];return t="["+Date()+"] "+t,console[e].apply(console,r.length?__spreadArray(__spreadArray([">".repeat(60)+"\n"+t+"\n"],r),["\n"+t+"\n"+"<".repeat(60)]):[t])},[4,Promise.resolve("").then(function(e){return new Promise(function(o,i){var e=s.resolve(__dirname,"../../../../logs/experience-app-build-"+(new Date).toISOString().split("T").shift()+".log");r.exec('npx webpack --config="./webpack/webpack-experience-app-build.config.js" --expAppId="'+a+'" >> "'+e+'" 2>&1',{cwd:s.resolve(__dirname,"../../../../../client"),timeout:1e6},function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var r=e[0],s=e[1],n=e[2];s&&console.log("["+Date()+"] stdout: \n"+s),n&&console.error("["+Date()+"] stderr: \n"+n),r?(u("error","========== buildItem ========== FAILED for AppId "+a,r),i(e)):(u("info","========== buildItem ========== SUCCEEDED for AppId "+a+"!"),o(s))})})}).then(function(e){var t=utils_1.getOwnerFromInfo(a),e={folder:a,itemId:a,owner:t,success:!0,result:e.toString()};utils_1.commonResponse(o,e)}).catch(function(e){t.error.message="build failed",t.error.itemId=a,t.error.detail=e,o.body=t})];case 2:return e.sent(),[3,4];case 3:return n=e.sent(),t.error.message=n,utils_1.writeResponseLog(n,!0),o.body=t,[3,4];case 4:return[4,i()];case 5:return e.sent(),[2]}})})}exports.addItem=addItem,exports.updateAppItem=updateAppItem,exports.getItemData=getItemData,exports.items=items,exports.searchItem=searchItem,exports.removeItem=removeItem,exports.addResources=addResources,exports.updateResources=updateResources,exports.resources=resources,exports.removeResources=removeResources,exports.buildItem=buildItem;