forked from LangGraph-GUI/LangGraph-GUI-reactflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileTransmit.js
128 lines (111 loc) · 3.96 KB
/
FileTransmit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// FileTransmit.js
import React, { useRef } from 'react';
import SERVER_URL from '../config';
import ConfigManager from '../ConfigManager';
function FileTransmit({ onUploadComplete }) {
const fileInputRef = useRef();
// Get the username from ConfigManager
const { username } = ConfigManager.getSettings();
const handleUploadClick = () => {
fileInputRef.current.click();
};
const handleFileChange = async (event) => {
const files = event.target.files;
if (files.length > 0) {
const formData = new FormData();
for (const file of files) {
formData.append('files', file);
}
try {
// Use the username in the API path
const response = await fetch(`${SERVER_URL}/upload/${encodeURIComponent(username)}`, {
method: 'POST',
body: formData,
});
if (response.ok) {
alert('Files successfully uploaded');
if (onUploadComplete) {
onUploadComplete();
}
} else {
const errorData = await response.json();
alert('Upload failed: ' + errorData.error);
}
} catch (error) {
alert('Upload failed: ' + error.message);
} finally {
event.target.value = null;
}
}
};
const handleDownloadClick = async () => {
try {
// Send GET request to the backend to download the zip file
const response = await fetch(`${SERVER_URL}/download/${encodeURIComponent(username)}`);
if (response.ok) {
// Create a Blob from the response data (the zip file)
const blob = await response.blob();
// Create a download link and simulate a click to trigger the download
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${username}_workspace.zip`; // Set the default file name
a.click();
// Clean up the object URL
URL.revokeObjectURL(url);
} else {
const errorData = await response.json();
alert('Download failed: ' + errorData.error);
}
} catch (error) {
alert('Download failed: ' + error.message);
}
};
// function to handle cache cleanup with username
const handleCleanCacheClick = async () => {
try {
// Use the username in the API path
const response = await fetch(`${SERVER_URL}/clean-cache/${encodeURIComponent(username)}`, {
method: 'POST'
});
if (response.ok) {
alert('Cache successfully cleaned');
} else {
const errorData = await response.json();
alert('Clean cache failed: ' + errorData.error);
}
} catch (error) {
alert('Clean cache failed: ' + error.message);
}
};
// Check if username is valid
const isUsernameValid = username && username.length > 0;
return (
<div className="flex flex-col">
<div className="flex space-x-2 mb-2">
<input
type="file"
ref={fileInputRef}
className="hidden"
onChange={handleFileChange}
multiple
/>
<button onClick={handleUploadClick} className="bg-blue-500 hover:bg-blue-700 text-white font-semibold px-1 rounded focus:outline-none focus:shadow-outline text-sm">
Upload Files to Server
</button>
<button onClick={handleDownloadClick} className="bg-green-500 hover:bg-green-700 text-white font-semibold px-1 rounded focus:outline-none focus:shadow-outline text-sm">
Get Files from Server
</button>
<button onClick={handleCleanCacheClick} className="bg-yellow-500 hover:bg-yellow-700 text-white font-semibold px-1 rounded focus:outline-none focus:shadow-outline text-sm">
Clean Server Cache
</button>
<div
className={`font-bold ${isUsernameValid ? 'text-green-500' : 'text-red-500'}`}
>
{isUsernameValid ? `User: ${username}` : 'User: undefined'}
</div>
</div>
</div>
);
}
export default FileTransmit;