Skip to content

Commit 6cdacb3

Browse files
author
Iurii Karpov
committed
TM-119: Добавить атачменты к задачам
1 parent 2d1f644 commit 6cdacb3

5 files changed

Lines changed: 711 additions & 305 deletions

File tree

‎src/actions/tmgr/files.ts‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import $axios from '@/plugins/axios';
2+
import { requestCache } from '@/utils/requestCache';
3+
4+
export interface TaskFile {
5+
id: number;
6+
name: string;
7+
original_name: string | null;
8+
file_path: string;
9+
mime_type: string | null;
10+
size: number | null;
11+
task_id: number;
12+
user_id: number;
13+
workspace_id: number;
14+
created_at: string | null;
15+
}
16+
17+
export const getFileType = (mimeType: string | null, fileName: string): 'image' | 'video' | 'pdf' | 'text' | 'other' => {
18+
const ext = fileName.split('.').pop()?.toLowerCase() || '';
19+
20+
if (ext === 'pdf' || mimeType === 'application/pdf') return 'pdf';
21+
22+
if (mimeType) {
23+
if (mimeType.startsWith('image/')) return 'image';
24+
if (mimeType.startsWith('video/')) return 'video';
25+
if (mimeType.startsWith('text/') || mimeType === 'application/json') return 'text';
26+
}
27+
28+
if (['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp'].includes(ext)) return 'image';
29+
if (['mp4', 'webm', 'ogg', 'mov', 'avi', 'mkv'].includes(ext)) return 'video';
30+
if (['txt', 'md', 'json', 'js', 'ts', 'css', 'html', 'xml', 'yaml', 'yml', 'log', 'csv'].includes(ext)) return 'text';
31+
32+
return 'other';
33+
};
34+
35+
export const isPreviewable = (mimeType: string | null, fileName: string): boolean => {
36+
const type = getFileType(mimeType, fileName);
37+
return type !== 'other';
38+
};
39+
40+
export const getTaskFiles = async (
41+
taskId: number,
42+
useCache: boolean = false,
43+
): Promise<TaskFile[]> => {
44+
const cacheKey = `files-task-${taskId}`;
45+
46+
if (useCache) {
47+
const cached = requestCache.get<TaskFile[]>(cacheKey);
48+
if (cached) {
49+
return cached;
50+
}
51+
}
52+
53+
const {
54+
data: { data },
55+
} = await $axios.get(`/tasks/${taskId}/files`);
56+
57+
if (useCache) {
58+
requestCache.set(cacheKey, data, 30000);
59+
}
60+
61+
return data;
62+
};
63+
64+
export const uploadTaskFile = async (
65+
taskId: number,
66+
file: File,
67+
): Promise<TaskFile> => {
68+
const formData = new FormData();
69+
formData.append('file', file);
70+
formData.append('name', file.name);
71+
72+
const { data } = await $axios.post(`/tasks/${taskId}/files`, formData, {
73+
headers: {
74+
'Content-Type': 'multipart/form-data',
75+
},
76+
});
77+
78+
requestCache.invalidate(`files-task-${taskId}`);
79+
80+
return data;
81+
};
82+
83+
export const deleteTaskFile = async (
84+
fileId: number,
85+
taskId?: number,
86+
): Promise<void> => {
87+
await $axios.delete(`/files/${fileId}`);
88+
89+
if (taskId) {
90+
requestCache.invalidate(`files-task-${taskId}`);
91+
}
92+
};
93+
94+
export const downloadTaskFile = async (fileId: number, fileName: string): Promise<void> => {
95+
const response = await $axios.get(`/files/${fileId}?download=1`, {
96+
responseType: 'blob',
97+
});
98+
99+
const blob = response.data instanceof Blob ? response.data : new Blob([response.data]);
100+
const url = window.URL.createObjectURL(blob);
101+
const link = document.createElement('a');
102+
link.href = url;
103+
link.setAttribute('download', fileName);
104+
document.body.appendChild(link);
105+
link.click();
106+
link.remove();
107+
window.URL.revokeObjectURL(url);
108+
};
109+
110+
export const getFilePreviewUrl = async (fileId: number, mimeType?: string | null): Promise<string> => {
111+
const response = await $axios.get(`/files/${fileId}`, {
112+
responseType: 'blob',
113+
});
114+
115+
const blob = response.data instanceof Blob
116+
? response.data
117+
: new Blob([response.data], mimeType ? { type: mimeType } : undefined);
118+
119+
return window.URL.createObjectURL(blob);
120+
};
121+
122+
export const getFileTextContent = async (fileId: number): Promise<string> => {
123+
const response = await $axios.get(`/files/${fileId}`, {
124+
responseType: 'text',
125+
});
126+
127+
return response.data;
128+
};

0 commit comments

Comments
 (0)