/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/pkgcloud-container-copy@0.5.5/pkgcloudContainerCopy.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
var pkgcloud=require("pkgcloud"),AWS=require("aws-sdk"),path=require("path"),fs=require("fs"),when=require("when"),nodefn=require("when/node"),md5=require("MD5"),stream=require("stream"),mkdirp=require("mkdirp"),url=require("url"),mime=require("mime-types"),pcc={copyContainer:function(e,n){var t=0,r=[];return when.unfold((function(){return t>=5?when.reject("transfer not completed, files on destination do not match the source"):pcc._copyContainer(e,n,t++).then((function(e){return when.filter((function(e){return null!==e}),e)})).then((function(e){return r=r.concat(e),when.resolve([null,0===e.length])}))}),(function(e){return e}),(function(){})).then((function(){var e=r.map((function(e){return e.name}));return r.filter((function(n,t){return e.lastIndexOf(n.name)===t}))}))},_copyContainer:function(e,n,t){var r=pcc.getFileList(e).catch((function(e){console.log("source error:",e)})),i=pcc.getFileList(n).catch((function(e){console.log("destination error:",e)}));return when.join(r,i).then((function(r){var i=pcc.getTransferPlan(r[0],r[1]),o=[];i.created.forEach((function(r){r.isDir?o.push({file:r,action:function(e){return pcc.createDir(n,e).then((function(e){return e&&process.stdout.write("created: "+e.name+path.sep+(t>0?" retry "+t:"")+"\n"),e}))}}):o.push({file:r,action:function(r){return pcc.transferFile(e,n,r).then((function(e){return e&&process.stdout.write("created: "+e.name+" "+e.size+(t>0?" retry "+t:"")+"\n"),e}))}})})),i.modified.forEach((function(r){o.push({file:r,action:function(r){return pcc.transferFile(e,n,r).then((function(e){return e&&process.stdout.write("modified: "+e.name+" "+e.size+(t>0?" retry "+t:"")+"\n"),e}))}})})),i.touched.forEach((function(e){})),i.deleted.reverse().forEach((function(e){e.isDir?o.push({file:e,action:function(e){return pcc.deleteDir(n,e).then((function(e){return e&&process.stdout.write("deleted: "+e.name+path.sep+(t>0?" retry "+t:"")+"\n"),e}))}}):o.push({file:e,action:function(e){return pcc.deleteFile(n,e).then((function(e){return e&&process.stdout.write("deleted: "+e.name+(t>0?" retry "+t:"")+"\n"),e}))}})}));var c=pcc.getHostname(e),a=pcc.getHostname(n);return null!==c&&c===a?when.reduce(o,(function(e,n){return n.action(n.file).then((function(n){return e.push(n),e}))}),[]):when.all(when.map(o,(function(e){return e.action(e.file)})))})).catch((function(e){console.log("error:",e)}))},transferFile:function(e,n,t){return when.promise((function(r,i){var o=pcc.getDestinationStream(n,t),c=pcc.getSourceStream(e,t);c.pipe(o),o.on("finish",(function(){r(t)})),o.on("success",(function(){r(t)})),o.on("error",(function(e){console.log("destination error:",t.name),i(e)})),c.on("error",(function(e){console.log("source error:",t.name),i(e)}))}))},createCloudContainerSpecifer:function(e,n,t){void 0===n&&(n=e.container,t=e.namePrefix,e=e.client),void 0===t&&(t="");var r={client:"aws-sdk"===e.provider?new AWS.S3({region:e.region,params:{Bucket:n,EncodingType:"url",Prefix:t}}):"rackspace"===e.provider||"amazon"===e.provider||void 0===e.download?pkgcloud.storage.createClient(e):e,container:n,namePrefix:t,delete:{enabled:!e.delete||void 0===e.delete.enabled||e.delete.enabled}};return e.meta&&(r.meta=e.meta),r},getSourceStream:function(e,n){if("string"==typeof e)return fs.createReadStream(path.resolve(e,n.name));if(e.client instanceof AWS.S3)return e.client.getObject({Key:e.namePrefix+n.name}).createReadStream();var t=e.client,r=e.container;return t.download({container:r,remote:n.name},(function(e){e&&console.log("source callback error:",e)}))},getDestinationStream:function(e,n){var t=new stream.PassThrough;if("string"==typeof e){var r=path.resolve(e,n.name);return nodefn.lift(mkdirp)(path.dirname(r)).then((function(){t.pipe(fs.createWriteStream(r))})),t}if(e.client instanceof AWS.S3){var i={};if(e.meta){var o={"Cache-Control":"CacheControl","Content-Encoding":"ContentEncoding"},c=e.meta;if(c.path&&c.default)for(var a in c.default)c.default.hasOwnProperty(a)&&(o[a]?i[o[a]]=c.default[a]:i[a]=c.default[a]);if(c.path&&c.path[n.name])for(var a in c.path[n.name])c.path[n.name].hasOwnProperty(a)&&(o[a]?i[o[a]]=c.path[n.name][a]:i[a]=c.path[n.name][a])}return i.Key=e.namePrefix+n.name,i.Body=t,i.ContentLength=n.size,i.ContentType=mime.lookup(n.name)||"application/octet-stream",e.client.putObject(i,(function(e){e&&console.log("destination callback error:",e)})),t}var u=e.client,l=e.container,s=u.upload({container:l,remote:n.name},(function(e){e&&console.log("destination callback error:",e)}));return t.pipe(s),t},createDir:function(e,n){return"string"==typeof e?nodefn.lift(mkdirp)(path.resolve(e,n.name)).then((function(){return n})):(e.client,AWS.S3,when.resolve(n))},deleteDir:function(e,n){if("string"==typeof e){var t=0;return when.unfold((function(){return++t>20?when.reject("directory not empty "+n.name):nodefn.lift(fs.readdir).bind(fs)(path.resolve(e,n.name)).then((function(t){return t.length?when.resolve([null,!1]).delay(100):nodefn.lift(fs.rmdir).bind(fs)(path.resolve(e,n.name))})).catch((function(e){return"ENOENT"!==e.code?when.reject(e):[null,!0]})).then((function(){return[null,!0]}))}),(function(e){return e}),(function(){})).then((function(){return n}))}return e.delete.enabled?(e.client,AWS.S3,when.resolve(n)):when.resolve(null)},deleteFile:function(e,n){if("string"==typeof e)return nodefn.lift(fs.unlink).bind(fs)(path.resolve(e,n.name)).then((function(){return n}));if(!e.delete.enabled)return when.resolve(null);if(e.client instanceof AWS.S3)return nodefn.lift(e.client.deleteObject).bind(e.client)({Key:e.namePrefix+n.name}).then((function(){return n}));var t=e.client,r=e.container;return nodefn.lift(t.removeFile).bind(t)(r,n.name).then((function(){return n}))},getHostname:function(e){return"string"==typeof e?null:e.client instanceof AWS.S3?e.client.endpoint.hostname:e.client&&e.client._serviceUrl?url.parse(e.client._serviceUrl).hostname:null},sort:function(e,n){return e.name===n.name?0:e.name>n.name?1:-1},getTransferPlan:function(e,n){var t={created:[],modified:[],touched:[],deleted:[],unchanged:[]};e=e.sort(pcc.sort),n=n.sort(pcc.sort);for(var r=0,i=0;r<e.length||i<n.length;)if(i!==n.length)if(r!==e.length){var o=e[r],c=n[i];if(-1!==pcc.sort(o,c))if(1!==pcc.sort(o,c))o.name===c.name&&o.isDir^c.isDir&&(t.created.push(o),t.deleted.push(c)),o.isDir||c.isDir||o.size===c.size&&null!==o.etag&&null!==c.etag&&o.etag===c.etag?o.isDir||c.isDir||(o.lastModified,c.lastModified,1)?t.unchanged.push(o):t.touched.push(o):t.modified.push(o),r++,i++;else{if(c.isDir&&o.name.substring(0,c.name.length+1)===c.name+path.sep){i++;continue}t.deleted.push(c),i++}else{if(o.isDir&&c.name.substring(0,o.name.length+1)===o.name+path.sep){r++;continue}t.created.push(o),r++}}else t.deleted.push(n[i]),i++;else t.created.push(e[r]),r++;return t},readdirRecurse:function(e){var n=[],t=path.resolve(e)+path.sep,r=function(e){var i=nodefn.lift(fs.readdir).bind(fs)(e);return i=when.map(i,(function(i){var o=path.resolve(e,i);return nodefn.lift(fs.stat).bind(fs)(o).then((function(e){if(e.isDirectory())return n.push({name:o.substring(t.length),isDir:!0,location:"local"}),r(o);n.push({name:o.substring(t.length),lastModified:e.mtime,size:e.size,etag:md5(fs.readFileSync(o)),isDir:!1,location:"local"})}))})),when.all(i)};return r(e).then((function(){return n}))},getFileList:function(e){var n;if("string"==typeof e)return pcc.readdirRecurse(e);if(e.client instanceof AWS.S3)return n=(n=nodefn.lift(e.client.listObjects).bind(e.client)({Prefix:e.namePrefix})).then((function(e){return e.Contents})),n=when.filter(n,(function(e){return e.Key.substr(-1)!==path.sep})),n=when.map(n,(function(n){return pcc.cloudFileModel(n,e.namePrefix)}));var t=e.client,r=e.container;return n=nodefn.lift(t.getFiles).bind(t)(r).catch((function(e){console.log("get file list error:",e)})),n=when.map(n,(function(e){return pcc.cloudFileModel(e,"")}))},cloudFileModel:function(e,n){var t=e.etag||e.ETag?e.etag||e.ETag.replace('"',"").replace('"',""):null;return{name:e.name||e.Key.substring(n.length),lastModified:e.lastModified||e.LastModified,size:e.size||e.Size,etag:t,isDir:!1,location:"remote"}}};module.exports=pcc;
//# sourceMappingURL=/sm/205b7161ae8b7a30492ae7b97ca46831b8f42d2cc0821fc97e5e3a22a7b7f29c.map