u
This commit is contained in:
109
src/api/vfs.js
Normal file
109
src/api/vfs.js
Normal file
@@ -0,0 +1,109 @@
|
||||
import { http, clientIdHeaders } from './http.js';
|
||||
|
||||
export const vfs = {
|
||||
/**
|
||||
* GET /api/vfs
|
||||
* List files in the virtual filesystem for the selected company.
|
||||
* → 200 { files: [{ name, size, mime, updated_at, created_at, isText, isImage, isPdf }] }
|
||||
*/
|
||||
list() {
|
||||
return http('/api/vfs');
|
||||
},
|
||||
|
||||
/**
|
||||
* GET /api/vfs/*path
|
||||
* Fetch raw content of a file (e.g. text for the editor).
|
||||
* @param {string} path
|
||||
*/
|
||||
async getText(path) {
|
||||
const clean = String(path || '').replace(/^\/+/, '');
|
||||
const res = await fetch(`/api/vfs/${clean}?raw=1`, {
|
||||
credentials: 'include',
|
||||
});
|
||||
if (!res.ok) {
|
||||
throw new Error(`Datei konnte nicht geladen werden (${res.status} ${res.statusText})`);
|
||||
}
|
||||
return res.text();
|
||||
},
|
||||
|
||||
/**
|
||||
* PUT /api/vfs/*path
|
||||
* Create or update a file.
|
||||
* @param {string} path
|
||||
* @param {string} content
|
||||
* @param {string} [mime]
|
||||
*/
|
||||
save(path, content, mime) {
|
||||
const clean = String(path || '').replace(/^\/+/, '');
|
||||
return http(`/api/vfs/${clean}`, {
|
||||
method: 'PUT',
|
||||
body: { content, mime },
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* DELETE /api/vfs/*path
|
||||
* Delete a file.
|
||||
* @param {string} path
|
||||
*/
|
||||
delete(path) {
|
||||
const clean = String(path || '').replace(/^\/+/, '');
|
||||
return http(`/api/vfs/${clean}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* POST /api/vfs/upload
|
||||
* Upload multiple files or a single file to VFS.
|
||||
* @param {File[]|FileList|File} files
|
||||
* @param {string} [targetDir]
|
||||
*/
|
||||
async upload(files, targetDir = '') {
|
||||
const formData = new FormData();
|
||||
if (targetDir) formData.append('targetDir', targetDir);
|
||||
const list = Array.isArray(files) ? files : files instanceof FileList ? Array.from(files) : [files];
|
||||
for (const f of list) {
|
||||
formData.append('files', f);
|
||||
}
|
||||
const res = await fetch('/api/vfs/upload', {
|
||||
method: 'POST',
|
||||
headers: { ...clientIdHeaders() },
|
||||
credentials: 'include',
|
||||
body: formData,
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
throw new Error(err.error || `Upload fehlgeschlagen (${res.status})`);
|
||||
}
|
||||
return res.json();
|
||||
},
|
||||
|
||||
/**
|
||||
* POST /api/vfs/mkdir
|
||||
* Create a directory in VFS.
|
||||
* @param {string} path
|
||||
*/
|
||||
mkdir(path) {
|
||||
const clean = String(path || '').replace(/^\/+/, '');
|
||||
return http('/api/vfs/mkdir', {
|
||||
method: 'POST',
|
||||
body: { path: clean },
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* POST /api/vfs/rename
|
||||
* Rename or move a file or directory in VFS.
|
||||
* @param {string} oldPath
|
||||
* @param {string} newPath
|
||||
*/
|
||||
rename(oldPath, newPath) {
|
||||
const cleanOld = String(oldPath || '').replace(/^\/+/, '');
|
||||
const cleanNew = String(newPath || '').replace(/^\/+/, '');
|
||||
return http('/api/vfs/rename', {
|
||||
method: 'POST',
|
||||
body: { oldPath: cleanOld, newPath: cleanNew },
|
||||
});
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user