@soga/encoder 0.0.5 → 0.0.6

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";var __importDefault=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ImgEncoder=void 0;const path_1=require("path"),common_1=require("../common/common"),sharp_1=__importDefault(require("sharp")),utils_1=require("@soga/utils"),main_1=require("../types/main"),types_1=require("@soga/types");class ImgEncoder extends common_1.Common{getCompressFileName(){return"img_compress.png"}getCompressFilePath(){return(0,path_1.resolve)(this.params.outputRoot,this.getCompressFileName())}getGzFileName(){return"preview_0"}getGzFilePath(){return(0,path_1.resolve)(this.params.outputRoot,this.getGzFileName())}async start(){try{return await this.compress(),await this.gzip(),await this.getData()}catch(t){await this.throwError({code:10400,message:t.message,stack:t.stack})}}async getData(){const t="img_encoder_get_data",e=await this.params.db.get(t);if(e)return e;const a=this.params.filePath,i=await(0,utils_1.getFileSize)(a),{width:s,height:r}=await(0,sharp_1.default)(a).metadata(),h=this.getCompressFilePath(),{width:o,height:p}=await(0,sharp_1.default)(h).metadata(),m=await(0,utils_1.getFileSize)(h),l=this.getGzFilePath(),n=await(0,utils_1.getFileSize)(l),u=await(0,utils_1.calculateMd5)({file:l,start:0,end:i-1}),c={start:0,end:n-1,size:i,file:this.getGzFileName(),path:l,index:0,md5:u};if(this.params.hosts.includes(types_1.HostType.BAIDU)){const t=await(0,utils_1.calculateMd4)({file:l,start:0,end:i-1});c.md4=t}if(this.params.hosts.includes(types_1.HostType.ALI)){const t=await(0,utils_1.calculateSha1)({file:l,start:0,end:i-1});c.sha1=t}const g={meta:{width:s,height:r,size:i,t_width:o,t_height:p,t_size:m},parts:[c]};return await this.params.db.put(t,g),g}async compress(){const t="img_encoder_compress";if(await this.params.db.get(t))return;const e=this.getCompressFilePath(),a=await(0,sharp_1.default)(this.params.filePath).metadata();if(a.width/a.height>1){const t=Math.min(640,a.height);await(0,sharp_1.default)(this.params.filePath).resize({height:t}).png({quality:75}).toFile(e)}else{const t=Math.min(640,a.width);await(0,sharp_1.default)(this.params.filePath).resize({width:t}).png({quality:75}).toFile(e)}await this.params.db.put(t,!0)}async gzip(){const t="img_encoder_gzip";if(await this.params.db.get(t))return;const e=this.getCompressFilePath(),a=this.getGzFilePath();await(0,utils_1.gzip)(e,a),await this.params.db.put(t,!0),await this.postProgress({type:main_1.ProcessStep.transcode_img,percent:1})}}exports.ImgEncoder=ImgEncoder;
1
+ "use strict";var __importDefault=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ImgEncoder=void 0;const path_1=require("path"),common_1=require("../common/common"),sharp_1=__importDefault(require("sharp")),utils_1=require("@soga/utils"),main_1=require("../types/main"),types_1=require("@soga/types");class ImgEncoder extends common_1.Common{getCompressFileName(){return"img_compress.png"}getCompressFilePath(){return(0,path_1.resolve)(this.params.outputRoot,this.getCompressFileName())}getGzFileName(){return"preview_0"}getGzFilePath(){return(0,path_1.resolve)(this.params.outputRoot,this.getGzFileName())}async start(){try{return await this.compress(),await this.gzip(),await this.getData()}catch(t){await this.throwError({code:10400,message:t.message,stack:t.stack})}}async getData(){const t="img_encoder_get_data",e=await this.params.db.get(t);if(e)return e;const a=this.params.filePath,i=await(0,utils_1.getFileSize)(a),{width:s,height:r}=await(0,sharp_1.default)(a).metadata(),h=this.getCompressFilePath(),{width:o,height:p}=await(0,sharp_1.default)(h).metadata(),m=await(0,utils_1.getFileSize)(h),l=this.getGzFilePath(),n=await(0,utils_1.getFileSize)(l),u=await(0,utils_1.calculateMd5)({file:l,start:0,end:i-1}),c={start:0,end:n-1,size:n,file:this.getGzFileName(),path:l,index:0,md5:u};if(this.params.hosts.includes(types_1.HostType.BAIDU)){const t=await(0,utils_1.calculateMd4)({file:l,start:0,end:n-1});c.md4=t}if(this.params.hosts.includes(types_1.HostType.ALI)){const t=await(0,utils_1.calculateSha1)({file:l,start:0,end:n-1});c.sha1=t}const g={meta:{width:s,height:r,size:i,t_width:o,t_height:p,t_size:m},parts:[c]};return await this.params.db.put(t,g),g}async compress(){const t="img_encoder_compress";if(await this.params.db.get(t))return;const e=this.getCompressFilePath(),a=await(0,sharp_1.default)(this.params.filePath).metadata();if(a.width/a.height>1){const t=Math.min(640,a.height);await(0,sharp_1.default)(this.params.filePath).resize({height:t}).png({quality:75}).toFile(e)}else{const t=Math.min(640,a.width);await(0,sharp_1.default)(this.params.filePath).resize({width:t}).png({quality:75}).toFile(e)}await this.params.db.put(t,!0)}async gzip(){const t="img_encoder_gzip";if(await this.params.db.get(t))return;const e=this.getCompressFilePath(),a=this.getGzFilePath();await(0,utils_1.gzip)(e,a),await this.params.db.put(t,!0),await this.postProgress({type:main_1.ProcessStep.transcode_img,percent:1})}}exports.ImgEncoder=ImgEncoder;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soga/encoder",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -50,7 +50,7 @@
50
50
  "@soga/common": "^0.1.6",
51
51
  "@soga/mediainfo": "^0.0.7",
52
52
  "@soga/types": "^0.0.61",
53
- "@soga/utils": "^0.0.10",
53
+ "@soga/utils": "^0.0.13",
54
54
  "chardet": "^2.0.0",
55
55
  "fluent-ffmpeg": "^2.1.3",
56
56
  "fs-extra": "^11.2.0",