You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.js 5.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /** Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. * =============================================================
  15. */
  16. import { bucketHost, bucketName } from '@/utils/minIO';
  17. import { isValidText, isValidVideo } from '@/utils/validate';
  18. import { generateUuid, performanceTiming } from '@/utils';
  19. const pMap = require('p-map');
  20. const minIOPrefix = `${process.env.VUE_APP_MINIO_API}/upload`;
  21. // const fileReaderStream = require('filereader-stream')
  22. // eslint-disable-next-line import/no-extraneous-dependencies
  23. const path = require('path');
  24. // 是否为视频文件
  25. const isValidVideoFile = file => {
  26. const extname = path.extname(file.name);
  27. return isValidVideo(extname);
  28. };
  29. const isValidTextFile = file => {
  30. const extname = path.extname(file.name);
  31. return isValidText(extname);
  32. };
  33. // 给图片名称添加时间戳
  34. export const hashName = (name) => {
  35. // 后缀名 .png
  36. const extname = path.extname(name);
  37. // 返回文件名称 test
  38. const basename = path.basename(name, extname);
  39. // 如果是视频文件,直接返回随机字符串名称(算法解析中文会有问题)
  40. const isVideo = isValidVideo(extname);
  41. // 避免重复添加后缀
  42. const filterBaseName = isVideo ? generateUuid(10) : basename.replace(/_ts\d+$/, '');
  43. return `${filterBaseName}_ts${performanceTiming()}${extname}`;
  44. };
  45. // minio 上传导致 chrome crash,自定义实现上传
  46. export const putObject = (uploadUrl, file, options = {}) => {
  47. const { callback, objectName, errCallback } = options;
  48. // 加载进度
  49. let loaded = 0;
  50. let total = 0;
  51. return new Promise((resolve, reject) => {
  52. const xhr = new XMLHttpRequest();
  53. xhr.open('PUT', uploadUrl, true);
  54. xhr.withCredentials = false;
  55. xhr.onload = e => {
  56. if (xhr.readyState === 4 && xhr.status === 200) {
  57. resolve({
  58. err: null,
  59. data: {
  60. objectName,
  61. result: generateUuid(32),
  62. },
  63. });
  64. } else {
  65. reject(e);
  66. }
  67. };
  68. if (typeof errCallback === 'function') {
  69. xhr.onerror = () => {
  70. errCallback(file);
  71. };
  72. }
  73. // todo: 视频进度loaded 解析会回滚,暂时不清楚原因
  74. xhr.upload.addEventListener('progress', event => {
  75. if (event.lengthComputable) {
  76. loaded = event.loaded;
  77. total = event.total;
  78. } else {
  79. // eslint-disable-next-line no-multi-assign
  80. loaded = total = event.total;
  81. }
  82. // 只解析视频进度
  83. if (typeof callback === 'function' && isValidVideoFile(file)) {
  84. callback(loaded, total);
  85. }
  86. }, false);
  87. xhr.send(file);
  88. });
  89. };
  90. // 默认通过 minIO 上传
  91. export const minIOUpload = async({ objectPath, fileList, transformFile }, callback, errCallback) => {
  92. // add 进度条
  93. let resolved = 0;
  94. const mapper = async d => {
  95. // 生成 stream 流
  96. // const blob = fileReaderStream(d.raw)
  97. const uploadPrefix = `${minIOPrefix}/${bucketName}`;
  98. const objectName = `${objectPath}/${d.name}`;
  99. const fileRes = await putObject(`${uploadPrefix}/${objectName}`, d.raw, {
  100. objectName,
  101. callback,
  102. errCallback,
  103. });
  104. // minIO 上传视频 chrome crash
  105. // const result = await window.minioClient.putObject(`${objectPath}/${d.name}`, blob, {
  106. // 'Content-Type': d.raw.type
  107. // })
  108. resolved+=1;
  109. // 进度反馈
  110. if (typeof callback === 'function' && fileList.length >= 1) {
  111. callback(resolved, fileList.length);
  112. }
  113. // 视频不做转换
  114. if (isValidVideoFile(d)) return fileRes;
  115. // 文本也不做转换
  116. if (isValidTextFile(d)) return fileRes;
  117. if (typeof transformFile === 'function') {
  118. const transformed = await transformFile(fileRes, d);
  119. return transformed;
  120. }
  121. return fileRes;
  122. };
  123. const result = await pMap(fileList, mapper, {concurrency: 10});
  124. return result;
  125. };
  126. export const renameFile = (name, options = {}) => {
  127. name = options.hash ? hashName(name) : name;
  128. name = options.encode ? encodeURIComponent(name) : name;
  129. return name;
  130. };
  131. export const getFileOutputPath = (rawFiles, { objectPath }) => {
  132. return rawFiles.map(d => `${bucketHost}/${bucketName}/${objectPath}/${d.name}`);
  133. };
  134. // 对文件进行自定义转换
  135. export const transformFile = (result, file) => {
  136. return new Promise((resolve) => {
  137. const reader = new FileReader();
  138. reader.addEventListener("load", () => {
  139. const img = new Image();
  140. img.onload = () => resolve({
  141. ...result,
  142. data: {
  143. ...result.data,
  144. meta: {
  145. width: img.width,
  146. height: img.height,
  147. },
  148. },
  149. });
  150. img.src = reader.result;
  151. }, false);
  152. reader.readAsDataURL(file.raw);
  153. });
  154. };

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能

Contributors (1)