2
0

files.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import axios from 'axios';
  2. import type { FileLibrary, FileCategory } from '../../share/types.ts';
  3. import type { MinioUploadPolicy, OSSUploadPolicy } from '@d8d-appcontainer/types';
  4. const API_BASE_URL = '/api';
  5. interface FileUploadPolicyResponse {
  6. message: string;
  7. data: MinioUploadPolicy | OSSUploadPolicy;
  8. }
  9. interface FileListResponse {
  10. message: string;
  11. data: {
  12. list: FileLibrary[];
  13. pagination: {
  14. current: number;
  15. pageSize: number;
  16. total: number;
  17. };
  18. };
  19. }
  20. interface FileSaveResponse {
  21. message: string;
  22. data: FileLibrary;
  23. }
  24. interface FileInfoResponse {
  25. message: string;
  26. data: FileLibrary;
  27. }
  28. interface FileDeleteResponse {
  29. message: string;
  30. }
  31. interface FileCategoryListResponse {
  32. data: FileCategory[];
  33. total: number;
  34. page: number;
  35. pageSize: number;
  36. }
  37. interface FileCategoryCreateResponse {
  38. message: string;
  39. data: FileCategory;
  40. }
  41. interface FileCategoryUpdateResponse {
  42. message: string;
  43. data: FileCategory;
  44. }
  45. interface FileCategoryDeleteResponse {
  46. message: string;
  47. }
  48. export const FileAPI = {
  49. getUploadPolicy: async (filename: string, prefix: string = 'uploads/', maxSize: number = 10 * 1024 * 1024): Promise<FileUploadPolicyResponse> => {
  50. try {
  51. const response = await axios.get(`${API_BASE_URL}/upload/policy`, {
  52. params: { filename, prefix, maxSize }
  53. });
  54. return response.data;
  55. } catch (error) {
  56. throw error;
  57. }
  58. },
  59. saveFileInfo: async (fileData: Partial<FileLibrary>): Promise<FileSaveResponse> => {
  60. try {
  61. const response = await axios.post(`${API_BASE_URL}/upload/save`, fileData);
  62. return response.data;
  63. } catch (error) {
  64. throw error;
  65. }
  66. },
  67. getFileList: async (params?: {
  68. page?: number,
  69. pageSize?: number,
  70. category_id?: number,
  71. fileType?: string,
  72. keyword?: string
  73. }): Promise<FileListResponse> => {
  74. try {
  75. const response = await axios.get(`${API_BASE_URL}/upload/list`, { params });
  76. return response.data;
  77. } catch (error) {
  78. throw error;
  79. }
  80. },
  81. getFileInfo: async (id: number): Promise<FileInfoResponse> => {
  82. try {
  83. const response = await axios.get(`${API_BASE_URL}/upload/${id}`);
  84. return response.data;
  85. } catch (error) {
  86. throw error;
  87. }
  88. },
  89. updateDownloadCount: async (id: number): Promise<FileDeleteResponse> => {
  90. try {
  91. const response = await axios.post(`${API_BASE_URL}/upload/${id}/download`);
  92. return response.data;
  93. } catch (error) {
  94. throw error;
  95. }
  96. },
  97. deleteFile: async (id: number): Promise<FileDeleteResponse> => {
  98. try {
  99. const response = await axios.delete(`${API_BASE_URL}/upload/${id}`);
  100. return response.data;
  101. } catch (error) {
  102. throw error;
  103. }
  104. },
  105. getCategories: async (params?: {
  106. page?: number,
  107. pageSize?: number,
  108. search?: string
  109. }): Promise<FileCategoryListResponse> => {
  110. try {
  111. const response = await axios.get(`${API_BASE_URL}/file-categories`, { params });
  112. return response.data;
  113. } catch (error) {
  114. throw error;
  115. }
  116. },
  117. createCategory: async (data: Partial<FileCategory>): Promise<FileCategoryCreateResponse> => {
  118. try {
  119. const response = await axios.post(`${API_BASE_URL}/file-categories`, data);
  120. return response.data;
  121. } catch (error) {
  122. throw error;
  123. }
  124. },
  125. updateCategory: async (id: number, data: Partial<FileCategory>): Promise<FileCategoryUpdateResponse> => {
  126. try {
  127. const response = await axios.put(`${API_BASE_URL}/file-categories/${id}`, data);
  128. return response.data;
  129. } catch (error) {
  130. throw error;
  131. }
  132. },
  133. deleteCategory: async (id: number): Promise<FileCategoryDeleteResponse> => {
  134. try {
  135. const response = await axios.delete(`${API_BASE_URL}/file-categories/${id}`);
  136. return response.data;
  137. } catch (error) {
  138. throw error;
  139. }
  140. }
  141. };