choose-and-upload-file.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. "use strict";
  2. const common_vendor = require("../../../common/vendor.js");
  3. const sheep_api_infra_file = require("../../api/infra/file.js");
  4. const ERR_MSG_OK = "chooseAndUploadFile:ok";
  5. const ERR_MSG_FAIL = "chooseAndUploadFile:fail";
  6. function chooseImage(opts) {
  7. const {
  8. count,
  9. sizeType = ["original", "compressed"],
  10. sourceType = ["album", "camera"],
  11. extension
  12. } = opts;
  13. return new Promise((resolve, reject) => {
  14. common_vendor.index.chooseImage({
  15. count,
  16. sizeType,
  17. sourceType,
  18. extension,
  19. success(res) {
  20. resolve(normalizeChooseAndUploadFileRes(res, "image"));
  21. },
  22. fail(res) {
  23. reject({
  24. errMsg: res.errMsg.replace("chooseImage:fail", ERR_MSG_FAIL)
  25. });
  26. }
  27. });
  28. });
  29. }
  30. function chooseVideo(opts) {
  31. const { camera, compressed, maxDuration, sourceType = ["album", "camera"], extension } = opts;
  32. return new Promise((resolve, reject) => {
  33. common_vendor.index.chooseVideo({
  34. camera,
  35. compressed,
  36. maxDuration,
  37. sourceType,
  38. extension,
  39. success(res) {
  40. const { tempFilePath, duration, size, height, width } = res;
  41. resolve(
  42. normalizeChooseAndUploadFileRes(
  43. {
  44. errMsg: "chooseVideo:ok",
  45. tempFilePaths: [tempFilePath],
  46. tempFiles: [
  47. {
  48. name: res.tempFile && res.tempFile.name || "",
  49. path: tempFilePath,
  50. size,
  51. type: res.tempFile && res.tempFile.type || "",
  52. width,
  53. height,
  54. duration,
  55. fileType: "video",
  56. cloudPath: ""
  57. }
  58. ]
  59. },
  60. "video"
  61. )
  62. );
  63. },
  64. fail(res) {
  65. reject({
  66. errMsg: res.errMsg.replace("chooseVideo:fail", ERR_MSG_FAIL)
  67. });
  68. }
  69. });
  70. });
  71. }
  72. function chooseAll(opts) {
  73. const { count, extension } = opts;
  74. return new Promise((resolve, reject) => {
  75. let chooseFile = common_vendor.index.chooseFile;
  76. if (typeof common_vendor.wx$1 !== "undefined" && typeof common_vendor.wx$1.chooseMessageFile === "function") {
  77. chooseFile = common_vendor.wx$1.chooseMessageFile;
  78. }
  79. if (typeof chooseFile !== "function") {
  80. return reject({
  81. errMsg: ERR_MSG_FAIL + " 请指定 type 类型,该平台仅支持选择 image 或 video。"
  82. });
  83. }
  84. chooseFile({
  85. type: "all",
  86. count,
  87. extension,
  88. success(res) {
  89. resolve(normalizeChooseAndUploadFileRes(res));
  90. },
  91. fail(res) {
  92. reject({
  93. errMsg: res.errMsg.replace("chooseFile:fail", ERR_MSG_FAIL)
  94. });
  95. }
  96. });
  97. });
  98. }
  99. function normalizeChooseAndUploadFileRes(res, fileType) {
  100. res.tempFiles.forEach((item, index) => {
  101. if (!item.name) {
  102. item.name = item.path.substring(item.path.lastIndexOf("/") + 1);
  103. }
  104. if (fileType) {
  105. item.fileType = fileType;
  106. }
  107. item.cloudPath = Date.now() + "_" + index + item.name.substring(item.name.lastIndexOf("."));
  108. });
  109. if (!res.tempFilePaths) {
  110. res.tempFilePaths = res.tempFiles.map((file) => file.path);
  111. }
  112. return res;
  113. }
  114. function uploadCloudFiles(files, max = 5, onUploadProgress) {
  115. files = JSON.parse(JSON.stringify(files));
  116. const len = files.length;
  117. let count = 0;
  118. let self = this;
  119. return new Promise((resolve) => {
  120. while (count < max) {
  121. next();
  122. }
  123. function next() {
  124. let cur = count++;
  125. if (cur >= len) {
  126. !files.find((item) => !item.url && !item.errMsg) && resolve(files);
  127. return;
  128. }
  129. const fileItem = files[cur];
  130. const index = self.files.findIndex((v) => v.uuid === fileItem.uuid);
  131. fileItem.url = "";
  132. delete fileItem.errMsg;
  133. common_vendor.Ws.uploadFile({
  134. filePath: fileItem.path,
  135. cloudPath: fileItem.cloudPath,
  136. fileType: fileItem.fileType,
  137. onUploadProgress: (res) => {
  138. res.index = index;
  139. onUploadProgress && onUploadProgress(res);
  140. }
  141. }).then((res) => {
  142. fileItem.url = res.fileID;
  143. fileItem.index = index;
  144. if (cur < len) {
  145. next();
  146. }
  147. }).catch((res) => {
  148. fileItem.errMsg = res.errMsg || res.message;
  149. fileItem.index = index;
  150. if (cur < len) {
  151. next();
  152. }
  153. });
  154. }
  155. });
  156. }
  157. function uploadFiles(choosePromise, { onChooseFile, onUploadProgress }) {
  158. return choosePromise.then((res) => {
  159. if (onChooseFile) {
  160. const customChooseRes = onChooseFile(res);
  161. if (typeof customChooseRes !== "undefined") {
  162. return Promise.resolve(customChooseRes).then(
  163. (chooseRes) => typeof chooseRes === "undefined" ? res : chooseRes
  164. );
  165. }
  166. }
  167. return res;
  168. }).then((res) => {
  169. if (res === false) {
  170. return {
  171. errMsg: ERR_MSG_OK,
  172. tempFilePaths: [],
  173. tempFiles: []
  174. };
  175. }
  176. return res;
  177. }).then(async (files) => {
  178. for (let file of files.tempFiles) {
  179. const { data } = await sheep_api_infra_file.FileApi.uploadFile(file.path);
  180. file.url = data;
  181. }
  182. return files;
  183. });
  184. }
  185. function chooseAndUploadFile(opts = {
  186. type: "all"
  187. }) {
  188. if (opts.type === "image") {
  189. return uploadFiles(chooseImage(opts), opts);
  190. } else if (opts.type === "video") {
  191. return uploadFiles(chooseVideo(opts), opts);
  192. }
  193. return uploadFiles(chooseAll(opts), opts);
  194. }
  195. exports.chooseAndUploadFile = chooseAndUploadFile;
  196. exports.uploadCloudFiles = uploadCloudFiles;