-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: add file upload button with progress bar and improve terminal session limits #753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yuezanhao
wants to merge
1
commit into
siteboon:main
Choose a base branch
from
yuezanhao:feat/upload-progress-terminal-enhancements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| import { useCallback, useState, useRef } from 'react'; | ||
| import type { Project } from '../../../types/app'; | ||
| import { api } from '../../../utils/api'; | ||
|
|
||
| type UseFileTreeUploadOptions = { | ||
| selectedProject: Project | null; | ||
|
|
@@ -57,6 +56,45 @@ const readAllDirectoryEntries = async (directoryEntry: FileSystemDirectoryEntry, | |
| return files; | ||
| }; | ||
|
|
||
| // Shared upload logic using XMLHttpRequest for progress tracking | ||
| const uploadFilesWithProgress = ( | ||
| url: string, | ||
| formData: FormData, | ||
| token: string | null, | ||
| onProgress: (percent: number) => void, | ||
| ): Promise<{ ok: boolean; status: number; json: () => Promise<unknown> }> => { | ||
| return new Promise((resolve, reject) => { | ||
| const xhr = new XMLHttpRequest(); | ||
| xhr.open('POST', url); | ||
|
|
||
| if (token) { | ||
| xhr.setRequestHeader('Authorization', `Bearer ${token}`); | ||
| } | ||
|
|
||
| xhr.upload.onprogress = (event) => { | ||
| if (event.lengthComputable) { | ||
| const percent = Math.round((event.loaded / event.total) * 100); | ||
| onProgress(percent); | ||
| } | ||
| }; | ||
|
|
||
| xhr.onload = () => { | ||
| const response = { | ||
| ok: xhr.status >= 200 && xhr.status < 300, | ||
| status: xhr.status, | ||
| json: () => Promise.resolve(JSON.parse(xhr.responseText)), | ||
| }; | ||
| resolve(response); | ||
| }; | ||
|
|
||
| xhr.onerror = () => { | ||
| reject(new Error('Network error during upload')); | ||
| }; | ||
|
|
||
| xhr.send(formData); | ||
| }); | ||
| }; | ||
|
|
||
| export const useFileTreeUpload = ({ | ||
| selectedProject, | ||
| onRefresh, | ||
|
|
@@ -65,8 +103,65 @@ export const useFileTreeUpload = ({ | |
| const [isDragOver, setIsDragOver] = useState(false); | ||
| const [dropTarget, setDropTarget] = useState<string | null>(null); | ||
| const [operationLoading, setOperationLoading] = useState(false); | ||
| const [uploadProgress, setUploadProgress] = useState(0); | ||
| const treeRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| const performUpload = useCallback(async (files: File[], targetPath: string) => { | ||
| if (files.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| if (!selectedProject) { | ||
| showToast('No project selected', 'error'); | ||
| return; | ||
| } | ||
|
|
||
| setOperationLoading(true); | ||
| setUploadProgress(0); | ||
|
|
||
| try { | ||
| const formData = new FormData(); | ||
| formData.append('targetPath', targetPath); | ||
|
|
||
| const relativePaths: string[] = []; | ||
| files.forEach((file) => { | ||
| const cleanFile = new File([file], file.name.split('/').pop()!, { | ||
| type: file.type, | ||
| lastModified: file.lastModified, | ||
| }); | ||
| formData.append('files', cleanFile); | ||
| relativePaths.push(file.name); | ||
| }); | ||
|
|
||
| formData.append('relativePaths', JSON.stringify(relativePaths)); | ||
|
|
||
| const token = localStorage.getItem('auth-token'); | ||
| const url = `/api/projects/${encodeURIComponent(selectedProject.projectId)}/files/upload`; | ||
|
|
||
| const response = await uploadFilesWithProgress( | ||
| url, | ||
| formData, | ||
| token, | ||
| (percent) => setUploadProgress(percent), | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| const data = (await response.json()) as { error?: string }; | ||
| throw new Error(data.error || 'Upload failed'); | ||
| } | ||
|
|
||
| showToast(`Uploaded ${files.length} file(s)`, 'success'); | ||
| onRefresh(); | ||
| } catch (err) { | ||
| console.error('Upload error:', err); | ||
| showToast(err instanceof Error ? err.message : 'Upload failed', 'error'); | ||
| } finally { | ||
| setOperationLoading(false); | ||
| setUploadProgress(0); | ||
| setDropTarget(null); | ||
| } | ||
| }, [selectedProject, onRefresh, showToast]); | ||
|
Comment on lines
+109
to
+163
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard
Suggested adjustment const [operationLoading, setOperationLoading] = useState(false);
const [uploadProgress, setUploadProgress] = useState(0);
+ const uploadInFlightRef = useRef(false);
const treeRef = useRef<HTMLDivElement>(null);
const performUpload = useCallback(async (files: File[], targetPath: string) => {
+ if (uploadInFlightRef.current) {
+ showToast('Upload already in progress', 'error');
+ return;
+ }
if (files.length === 0) {
return;
}
if (!selectedProject) {
showToast('No project selected', 'error');
return;
}
+ uploadInFlightRef.current = true;
setOperationLoading(true);
setUploadProgress(0);
try {
// ...
} finally {
setOperationLoading(false);
setUploadProgress(0);
setDropTarget(null);
+ uploadInFlightRef.current = false;
}
}, [selectedProject, onRefresh, showToast]);🤖 Prompt for AI Agents |
||
|
|
||
| const handleDragEnter = useCallback((e: React.DragEvent) => { | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
|
|
@@ -94,7 +189,6 @@ export const useFileTreeUpload = ({ | |
| setIsDragOver(false); | ||
|
|
||
| const targetPath = dropTarget || ''; | ||
| setOperationLoading(true); | ||
|
|
||
| try { | ||
| const files: File[] = []; | ||
|
|
@@ -129,54 +223,21 @@ export const useFileTreeUpload = ({ | |
| } | ||
|
|
||
| if (files.length === 0) { | ||
| setOperationLoading(false); | ||
| setDropTarget(null); | ||
| return; | ||
| } | ||
|
|
||
| const formData = new FormData(); | ||
| formData.append('targetPath', targetPath); | ||
|
|
||
| // Store relative paths separately since FormData strips path info from File.name | ||
| const relativePaths: string[] = []; | ||
| files.forEach((file) => { | ||
| // Create a new file with just the filename (without path) for FormData | ||
| // but store the relative path separately | ||
| const cleanFile = new File([file], file.name.split('/').pop()!, { | ||
| type: file.type, | ||
| lastModified: file.lastModified | ||
| }); | ||
| formData.append('files', cleanFile); | ||
| relativePaths.push(file.name); // Keep the full relative path | ||
| }); | ||
|
|
||
| // Send relative paths as a JSON array | ||
| formData.append('relativePaths', JSON.stringify(relativePaths)); | ||
|
|
||
| const response = await api.post( | ||
| // File upload endpoint is keyed by DB projectId post-migration. | ||
| `/projects/${encodeURIComponent(selectedProject!.projectId)}/files/upload`, | ||
| formData | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| const data = await response.json(); | ||
| throw new Error(data.error || 'Upload failed'); | ||
| } | ||
|
|
||
| showToast( | ||
| `Uploaded ${files.length} file(s)`, | ||
| 'success' | ||
| ); | ||
| onRefresh(); | ||
| await performUpload(files, targetPath); | ||
| } catch (err) { | ||
| console.error('Upload error:', err); | ||
| console.error('Drop error:', err); | ||
| showToast(err instanceof Error ? err.message : 'Upload failed', 'error'); | ||
| } finally { | ||
| setOperationLoading(false); | ||
| setDropTarget(null); | ||
| } | ||
| }, [dropTarget, selectedProject, onRefresh, showToast]); | ||
| }, [dropTarget, performUpload, showToast]); | ||
|
|
||
| // Handle file selection from the upload button's file input | ||
| const handleFileSelect = useCallback((files: File[], targetPath?: string) => { | ||
| performUpload(files, targetPath || ''); | ||
| }, [performUpload]); | ||
|
|
||
| const handleItemDragOver = useCallback((e: React.DragEvent, itemPath: string) => { | ||
| e.preventDefault(); | ||
|
|
@@ -194,11 +255,13 @@ export const useFileTreeUpload = ({ | |
| isDragOver, | ||
| dropTarget, | ||
| operationLoading, | ||
| uploadProgress, | ||
| treeRef, | ||
| handleDragEnter, | ||
| handleDragOver, | ||
| handleDragLeave, | ||
| handleDrop, | ||
| handleFileSelect, | ||
| handleItemDragOver, | ||
| handleItemDrop, | ||
| setDropTarget, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
200MB × 20 files allows multi-GB requests and temp-disk exhaustion.
Line 884 with Line 885 now permits up to ~4GB per request in
os.tmpdir(). That is a high outage risk under concurrent uploads.Suggested adjustment
📝 Committable suggestion
🤖 Prompt for AI Agents