frontend: replace superagent with pankow fetcher in DirectoryModel
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
|
||||
import superagent from 'superagent';
|
||||
import { fetcher } from 'pankow';
|
||||
import { sanitize } from 'pankow/utils';
|
||||
|
||||
const BASE_URL = import.meta.env.BASE_URL || '/';
|
||||
@@ -35,15 +35,15 @@ export function createDirectoryModel(origin, accessToken, api) {
|
||||
async listFiles(path) {
|
||||
let error, result;
|
||||
try {
|
||||
result = await superagent.get(`${origin}/api/v1/${api}/files/${path}`).query({ access_token: accessToken });
|
||||
result = await fetcher.get(`${origin}/api/v1/${api}/files/${path}`, { access_token: accessToken });
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
if (error || result.statusCode !== 200) {
|
||||
if (error || result.status !== 200) {
|
||||
if (error.status === 404) return [];
|
||||
|
||||
console.error('Failed to list files', error || result.statusCode);
|
||||
console.error('Failed to list files', error || result.status);
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -109,31 +109,22 @@ export function createDirectoryModel(origin, accessToken, api) {
|
||||
await this.save(filePath, '');
|
||||
},
|
||||
async newFolder(folderPath) {
|
||||
await superagent.post(`${origin}/api/v1/${api}/files/${folderPath}?access_token=${accessToken}&directory=true`);
|
||||
await fetcher.post(`${origin}/api/v1/${api}/files/${folderPath}`, { access_token: accessToken, directory: true });
|
||||
},
|
||||
async remove(filePath) {
|
||||
await superagent.del(`${origin}/api/v1/${api}/files/${filePath}`)
|
||||
.query({ access_token: accessToken });
|
||||
await fetcher.del(`${origin}/api/v1/${api}/files/${filePath}`, { access_token: accessToken });
|
||||
},
|
||||
async rename(fromFilePath, toFilePath, overwrite = false) {
|
||||
await superagent.put(`${origin}/api/v1/${api}/files/${fromFilePath}`)
|
||||
.send({ action: 'rename', newFilePath: sanitize(toFilePath), overwrite })
|
||||
.query({ access_token: accessToken });
|
||||
await fetcher.put(`${origin}/api/v1/${api}/files/${fromFilePath}`, { action: 'rename', newFilePath: sanitize(toFilePath), overwrite }, { access_token: accessToken });
|
||||
},
|
||||
async copy(fromFilePath, toFilePath) {
|
||||
await superagent.put(`${origin}/api/v1/${api}/files/${fromFilePath}`)
|
||||
.send({ action: 'copy', newFilePath: sanitize(toFilePath) })
|
||||
.query({ access_token: accessToken });
|
||||
await fetcher.put(`${origin}/api/v1/${api}/files/${fromFilePath}`, { action: 'copy', newFilePath: sanitize(toFilePath) }, { access_token: accessToken });
|
||||
},
|
||||
async chown(filePath, uid) {
|
||||
await superagent.put(`${origin}/api/v1/${api}/files/${filePath}`)
|
||||
.send({ action: 'chown', uid: uid, recursive: true })
|
||||
.query({ access_token: accessToken });
|
||||
await fetcher.put(`${origin}/api/v1/${api}/files/${filePath}`, { action: 'chown', uid: uid, recursive: true }, { access_token: accessToken });
|
||||
},
|
||||
async extract(path) {
|
||||
await superagent.put(`${origin}/api/v1/${api}/files/${path}`)
|
||||
.send({ action: 'extract' })
|
||||
.query({ access_token: accessToken });
|
||||
await fetcher.put(`${origin}/api/v1/${api}/files/${path}`, { action: 'extract' }, { access_token: accessToken });
|
||||
},
|
||||
async download(path) {
|
||||
window.open(`${origin}/api/v1/${api}/files/${path}?download=true&access_token=${accessToken}`);
|
||||
@@ -169,7 +160,7 @@ export function createDirectoryModel(origin, accessToken, api) {
|
||||
xhr.send(file);
|
||||
});
|
||||
|
||||
const res = await req;
|
||||
await req;
|
||||
},
|
||||
async getFile(path) {
|
||||
let result;
|
||||
@@ -185,7 +176,7 @@ export function createDirectoryModel(origin, accessToken, api) {
|
||||
},
|
||||
async paste(targetDir, action, files) {
|
||||
// this will not overwrite but tries to find a new unique name to past to
|
||||
for (let f in files) {
|
||||
for (const f in files) {
|
||||
let done = false;
|
||||
let targetPath = targetDir + '/' + files[f].name;
|
||||
while (!done) {
|
||||
|
||||
Reference in New Issue
Block a user