index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. "use strict";
  2. var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");
  3. _Object$defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.imageView2 = imageView2;
  7. exports.imageMogr2 = imageMogr2;
  8. exports.watermark = watermark;
  9. exports.imageInfo = imageInfo;
  10. exports.exif = exif;
  11. exports.pipeline = pipeline;
  12. var _utils = require("../utils");
  13. function getImageUrl(key, domain) {
  14. key = encodeURIComponent(key);
  15. if (domain.slice(domain.length - 1) !== '/') {
  16. domain += '/';
  17. }
  18. return domain + key;
  19. }
  20. function imageView2(op, key, domain) {
  21. if (!/^\d$/.test(String(op.mode))) {
  22. throw 'mode should be number in imageView2';
  23. }
  24. var mode = op.mode,
  25. w = op.w,
  26. h = op.h,
  27. q = op.q,
  28. format = op.format;
  29. if (!w && !h) {
  30. throw 'param w and h is empty in imageView2';
  31. }
  32. var imageUrl = 'imageView2/' + encodeURIComponent(mode);
  33. imageUrl += w ? '/w/' + encodeURIComponent(w) : '';
  34. imageUrl += h ? '/h/' + encodeURIComponent(h) : '';
  35. imageUrl += q ? '/q/' + encodeURIComponent(q) : '';
  36. imageUrl += format ? '/format/' + encodeURIComponent(format) : '';
  37. if (key && domain) {
  38. imageUrl = getImageUrl(key, domain) + '?' + imageUrl;
  39. }
  40. return imageUrl;
  41. } // invoke the imageMogr2 api of Qiniu
  42. function imageMogr2(op, key, domain) {
  43. var autoOrient = op['auto-orient'];
  44. var thumbnail = op.thumbnail,
  45. strip = op.strip,
  46. gravity = op.gravity,
  47. crop = op.crop,
  48. quality = op.quality,
  49. rotate = op.rotate,
  50. format = op.format,
  51. blur = op.blur;
  52. var imageUrl = 'imageMogr2';
  53. imageUrl += autoOrient ? '/auto-orient' : '';
  54. imageUrl += thumbnail ? '/thumbnail/' + encodeURIComponent(thumbnail) : '';
  55. imageUrl += strip ? '/strip' : '';
  56. imageUrl += gravity ? '/gravity/' + encodeURIComponent(gravity) : '';
  57. imageUrl += quality ? '/quality/' + encodeURIComponent(quality) : '';
  58. imageUrl += crop ? '/crop/' + encodeURIComponent(crop) : '';
  59. imageUrl += rotate ? '/rotate/' + encodeURIComponent(rotate) : '';
  60. imageUrl += format ? '/format/' + encodeURIComponent(format) : '';
  61. imageUrl += blur ? '/blur/' + encodeURIComponent(blur) : '';
  62. if (key && domain) {
  63. imageUrl = getImageUrl(key, domain) + '?' + imageUrl;
  64. }
  65. return imageUrl;
  66. } // invoke the watermark api of Qiniu
  67. function watermark(op, key, domain) {
  68. var mode = op.mode;
  69. if (!mode) {
  70. throw "mode can't be empty in watermark";
  71. }
  72. var imageUrl = 'watermark/' + mode;
  73. if (mode !== 1 && mode !== 2) {
  74. throw 'mode is wrong';
  75. }
  76. if (mode === 1) {
  77. var image = op.image;
  78. if (!image) {
  79. throw "image can't be empty in watermark";
  80. }
  81. imageUrl += image ? '/image/' + (0, _utils.urlSafeBase64Encode)(image) : '';
  82. }
  83. if (mode === 2) {
  84. var text = op.text,
  85. font = op.font,
  86. fontsize = op.fontsize,
  87. fill = op.fill;
  88. if (!text) {
  89. throw "text can't be empty in watermark";
  90. }
  91. imageUrl += text ? '/text/' + (0, _utils.urlSafeBase64Encode)(text) : '';
  92. imageUrl += font ? '/font/' + (0, _utils.urlSafeBase64Encode)(font) : '';
  93. imageUrl += fontsize ? '/fontsize/' + fontsize : '';
  94. imageUrl += fill ? '/fill/' + (0, _utils.urlSafeBase64Encode)(fill) : '';
  95. }
  96. var dissolve = op.dissolve,
  97. gravity = op.gravity,
  98. dx = op.dx,
  99. dy = op.dy;
  100. imageUrl += dissolve ? '/dissolve/' + encodeURIComponent(dissolve) : '';
  101. imageUrl += gravity ? '/gravity/' + encodeURIComponent(gravity) : '';
  102. imageUrl += dx ? '/dx/' + encodeURIComponent(dx) : '';
  103. imageUrl += dy ? '/dy/' + encodeURIComponent(dy) : '';
  104. if (key && domain) {
  105. imageUrl = getImageUrl(key, domain) + '?' + imageUrl;
  106. }
  107. return imageUrl;
  108. } // invoke the imageInfo api of Qiniu
  109. function imageInfo(key, domain) {
  110. var url = getImageUrl(key, domain) + '?imageInfo';
  111. return (0, _utils.request)(url, {
  112. method: 'GET'
  113. });
  114. } // invoke the exif api of Qiniu
  115. function exif(key, domain) {
  116. var url = getImageUrl(key, domain) + '?exif';
  117. return (0, _utils.request)(url, {
  118. method: 'GET'
  119. });
  120. }
  121. function pipeline(arr, key, domain) {
  122. var isArray = Object.prototype.toString.call(arr) === '[object Array]';
  123. var option;
  124. var errOp = false;
  125. var imageUrl = '';
  126. if (isArray) {
  127. for (var i = 0, len = arr.length; i < len; i++) {
  128. option = arr[i];
  129. if (!option.fop) {
  130. throw "fop can't be empty in pipeline";
  131. }
  132. switch (option.fop) {
  133. case 'watermark':
  134. imageUrl += watermark(option) + '|';
  135. break;
  136. case 'imageView2':
  137. imageUrl += imageView2(option) + '|';
  138. break;
  139. case 'imageMogr2':
  140. imageUrl += imageMogr2(option) + '|';
  141. break;
  142. default:
  143. errOp = true;
  144. break;
  145. }
  146. if (errOp) {
  147. throw 'fop is wrong in pipeline';
  148. }
  149. }
  150. if (key && domain) {
  151. imageUrl = getImageUrl(key, domain) + '?' + imageUrl;
  152. var length_1 = imageUrl.length;
  153. if (imageUrl.slice(length_1 - 1) === '|') {
  154. imageUrl = imageUrl.slice(0, length_1 - 1);
  155. }
  156. }
  157. return imageUrl;
  158. }
  159. throw "pipeline's first param should be array";
  160. }