file.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import axios from 'axios';
  2. export async function upload(
  3. blob: Blob,
  4. shared = false,
  5. filename = 'thumb.png',
  6. directory = '/大屏/图片/默认',
  7. conflict?:string
  8. ) {
  9. const form = new FormData();
  10. let name = filename;
  11. if(!name.endsWith('.png')) {
  12. name += '.png';
  13. }
  14. form.append('name', name);
  15. form.append('directory', directory);
  16. form.append('shared', shared + '');
  17. form.append('file', blob);
  18. // let ret;
  19. if(conflict) {
  20. // ret = await axios.post('/api/image/upload',{conflict}, form);
  21. form.append('conflict', conflict);
  22. }
  23. const ret = await axios.post('/api/image/upload', form);
  24. if (!ret) {
  25. return;
  26. }
  27. return ret;
  28. }
  29. export async function delImage(image: string) {
  30. const ret: any = await axios.delete('/api' + image);
  31. if (ret.error) {
  32. return false;
  33. }
  34. return true;
  35. }
  36. export async function addImage(image: string) {
  37. const ret: any = await axios.post('/api/user/image', { image });
  38. if (ret.error) {
  39. return '';
  40. }
  41. return ret.id;
  42. }
  43. export function filename(str: string) {
  44. const i = str.lastIndexOf('.');
  45. return str.substring(0, i);
  46. }
  47. const units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  48. export function formatBytes(size: number) {
  49. let l = 0;
  50. let n = size / 1024;
  51. while (n >= 1024 && ++l) {
  52. n = n / 1024;
  53. }
  54. return Math.round(n * 100) / 100 + ' ' + units[l];
  55. }
  56. /**
  57. * 读取文件内容,返回字符串
  58. * @param file 文件
  59. */
  60. export async function readFile(file: Blob) {
  61. return new Promise<string>((resolve, reject) => {
  62. const reader = new FileReader();
  63. reader.onload = () => {
  64. resolve(reader.result as string);
  65. };
  66. reader.onerror = reject;
  67. // readAsText 使 result 一定是字符串
  68. reader.readAsText(file);
  69. });
  70. }
  71. export function dataURLtoBlob(base64: string) {
  72. let arr: any = base64.split(','),
  73. mime = arr[0].match(/:(.*?);/)[1],
  74. bstr = atob(arr[1]),
  75. n = bstr.length,
  76. u8arr = new Uint8Array(n);
  77. while (n--) {
  78. u8arr[n] = bstr.charCodeAt(n);
  79. }
  80. return new Blob([u8arr], { type: mime });
  81. }
  82. /**
  83. * 图片转 Blob
  84. * @param img 图片
  85. */
  86. export function saveToBlob(img: HTMLImageElement): Blob {
  87. const canvas: HTMLCanvasElement = document.createElement('canvas');
  88. canvas.setAttribute('origin-clean', 'false');
  89. canvas.width = img.width;
  90. canvas.height = img.height;
  91. const context: any = canvas.getContext('2d');
  92. context.filter = window.getComputedStyle(img).filter;
  93. context.drawImage(img, 0, 0, canvas.width, canvas.height);
  94. return dataURLtoBlob(canvas.toDataURL());
  95. }