mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 15:44:21 +00:00
@@ -29,10 +29,16 @@ export const apiCall = async (
|
|||||||
// Set up headers with CSRF token for non-GET requests
|
// Set up headers with CSRF token for non-GET requests
|
||||||
const method = options.method || 'GET';
|
const method = options.method || 'GET';
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
'Content-Type': 'application/json',
|
|
||||||
...(options.headers as Record<string, string>),
|
...(options.headers as Record<string, string>),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Only set Content-Type to application/json if not already set and body is not FormData
|
||||||
|
// FormData requires the browser to set Content-Type with the boundary parameter
|
||||||
|
const isFormData = options.body instanceof FormData;
|
||||||
|
if (!headers['Content-Type'] && !isFormData) {
|
||||||
|
headers['Content-Type'] = 'application/json';
|
||||||
|
}
|
||||||
|
|
||||||
// Add CSRF token for non-GET methods
|
// Add CSRF token for non-GET methods
|
||||||
if (method !== 'GET') {
|
if (method !== 'GET') {
|
||||||
const csrfToken = getCsrfToken();
|
const csrfToken = getCsrfToken();
|
||||||
@@ -41,11 +47,18 @@ export const apiCall = async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For FormData, don't include Content-Type in headers - let the browser set it
|
||||||
|
const fetchHeaders = isFormData
|
||||||
|
? Object.fromEntries(
|
||||||
|
Object.entries(headers).filter(([key]) => key !== 'Content-Type')
|
||||||
|
)
|
||||||
|
: headers;
|
||||||
|
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
...options,
|
...options,
|
||||||
// Include credentials to send/receive cookies
|
// Include credentials to send/receive cookies
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
headers,
|
headers: fetchHeaders,
|
||||||
});
|
});
|
||||||
console.debug(`Response status: ${response.status} for URL: ${url}`);
|
console.debug(`Response status: ${response.status} for URL: ${url}`);
|
||||||
|
|
||||||
|
|||||||
@@ -122,7 +122,8 @@ export const useFileOperations = (): UseFileOperationsResult => {
|
|||||||
if (!currentWorkspace) return false;
|
if (!currentWorkspace) return false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await uploadFile(currentWorkspace.name, targetPath || '', files);
|
// Default to '.' (root directory) if no target path is provided
|
||||||
|
await uploadFile(currentWorkspace.name, targetPath || '.', files);
|
||||||
|
|
||||||
notifications.show({
|
notifications.show({
|
||||||
title: 'Success',
|
title: 'Success',
|
||||||
|
|||||||
@@ -410,7 +410,8 @@ func (h *Handler) UploadFile() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
filePath := decodedPath + "/" + formFile.Filename
|
// Use filepath.Join to properly construct the path
|
||||||
|
filePath := filepath.Join(decodedPath, formFile.Filename)
|
||||||
|
|
||||||
content, err := io.ReadAll(file)
|
content, err := io.ReadAll(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -352,15 +352,6 @@ func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
|
|||||||
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("upload with missing file_path parameter", func(t *testing.T) {
|
|
||||||
fileName := "test.txt"
|
|
||||||
fileContent := "test content"
|
|
||||||
files := map[string]string{fileName: fileContent}
|
|
||||||
|
|
||||||
rr := h.makeUploadRequest(t, baseURL+"/upload", files, h.RegularTestUser)
|
|
||||||
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("upload with invalid file_path", func(t *testing.T) {
|
t.Run("upload with invalid file_path", func(t *testing.T) {
|
||||||
fileName := "test.txt"
|
fileName := "test.txt"
|
||||||
fileContent := "test content"
|
fileContent := "test content"
|
||||||
|
|||||||
Reference in New Issue
Block a user