utils.ts 1006 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { uploadFileApi, UPLOAD_BIZ_TYPE } from '@/api/minio';
  2. import type { ImageItem } from './types';
  3. export function stringToArray(str?: string): number[] | undefined {
  4. if (!str) return undefined;
  5. return JSON.parse('[' + str + ']');
  6. }
  7. export function unformatImage(file?: string) {
  8. if (!file) return undefined;
  9. const fileData: ImageItem[] = JSON.parse(file);
  10. return fileData;
  11. }
  12. const formatImage = async (data: ImageItem) => {
  13. if (!data.file) return data;
  14. const fileName = data.file.name;
  15. const fileSize = data.size;
  16. const res = await uploadFileApi({ bizType: UPLOAD_BIZ_TYPE.ATTACHMENT, fileName, file: data.file });
  17. const fileUrl = res.url;
  18. return {
  19. fileName,
  20. fileSize,
  21. fileUrl,
  22. };
  23. };
  24. export const formatImageList = async (data: ImageItem[] | undefined) => {
  25. if (!data || data.length === 0) return null;
  26. const res = await Promise.all(
  27. data.map(async (item) => {
  28. const res = await formatImage(item);
  29. return res;
  30. }),
  31. );
  32. return res;
  33. };