parent
30c963a5f0
commit
61a1c93f0f
89 changed files with 15859 additions and 2 deletions
7
client/libs/__mocks__/api_auth.js
Normal file
7
client/libs/__mocks__/api_auth.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
export function login(serverAddr, adminId, adminPwd, axiosConfig) {
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
|
||||
export function logout(serverAddr, axiosConfig) {
|
||||
return Promise.resolve(true);
|
||||
}
|
41
client/libs/__mocks__/api_share.js
Normal file
41
client/libs/__mocks__/api_share.js
Normal file
|
@ -0,0 +1,41 @@
|
|||
let _infos = [];
|
||||
const shadowedId = "shadowedId";
|
||||
const publicId = "publicId";
|
||||
|
||||
export function __addInfos(infos) {
|
||||
_infos = [..._infos, ...infos];
|
||||
}
|
||||
|
||||
export function __truncInfos(info) {
|
||||
_infos = [];
|
||||
}
|
||||
|
||||
export const del = shareId => {
|
||||
_infos = _infos.filter(info => {
|
||||
return !info.shareId == shareId;
|
||||
});
|
||||
return Promise.resolve(true);
|
||||
};
|
||||
|
||||
export const list = () => {
|
||||
return Promise.resolve(_infos);
|
||||
};
|
||||
|
||||
export const shadowId = shareId => {
|
||||
return Promise.resolve(shadowedId);
|
||||
};
|
||||
|
||||
export const publishId = shareId => {
|
||||
return Promise.resolve(publicId);
|
||||
};
|
||||
|
||||
export const setDownLimit = (shareId, downLimit) => {
|
||||
_infos = _infos.map(info => {
|
||||
return info.shareId == shareId ? { ...info, downLimit } : info;
|
||||
});
|
||||
return Promise.resolve(true);
|
||||
};
|
||||
|
||||
export const addLocalFiles = () => {
|
||||
return Promise.resolve(true);
|
||||
};
|
29
client/libs/api_auth.js
Normal file
29
client/libs/api_auth.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
import axios from "axios";
|
||||
import { config } from "../config";
|
||||
import { makePostBody } from "./utils";
|
||||
|
||||
export function login(serverAddr, adminId, adminPwd, axiosConfig) {
|
||||
return axios
|
||||
.post(
|
||||
`${serverAddr}/login`,
|
||||
makePostBody(
|
||||
{
|
||||
act: "login",
|
||||
adminid: adminId,
|
||||
adminpwd: adminPwd
|
||||
},
|
||||
axiosConfig
|
||||
)
|
||||
)
|
||||
.then(response => {
|
||||
return response.data.Code === 200;
|
||||
});
|
||||
}
|
||||
|
||||
export function logout(serverAddr, axiosConfig) {
|
||||
return axios
|
||||
.post(`${serverAddr}/login`, makePostBody({ act: "logout" }), axiosConfig)
|
||||
.then(response => {
|
||||
return response.data.Code === 200;
|
||||
});
|
||||
}
|
51
client/libs/api_share.js
Normal file
51
client/libs/api_share.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
import axios from "axios";
|
||||
import { config } from "../config";
|
||||
|
||||
export const del = shareId => {
|
||||
return axios
|
||||
.delete(`${config.serverAddr}/fileinfo?shareid=${shareId}`)
|
||||
.then(response => response.data.Code === 200);
|
||||
};
|
||||
|
||||
export const list = () => {
|
||||
return axios.get(`${config.serverAddr}/fileinfo`).then(response => {
|
||||
// TODO check status code
|
||||
return response.data.List;
|
||||
});
|
||||
};
|
||||
|
||||
export const shadowId = shareId => {
|
||||
const act = "shadowid";
|
||||
return axios
|
||||
.patch(`${config.serverAddr}/fileinfo?act=${act}&shareid=${shareId}`)
|
||||
.then(response => {
|
||||
return response.data.ShareId;
|
||||
});
|
||||
};
|
||||
|
||||
export const publishId = shareId => {
|
||||
const act = "publishid";
|
||||
return axios
|
||||
.patch(`${config.serverAddr}/fileinfo?act=${act}&shareid=${shareId}`)
|
||||
.then(response => {
|
||||
return response.data.ShareId;
|
||||
});
|
||||
};
|
||||
|
||||
export const setDownLimit = (shareId, downLimit) => {
|
||||
const act = "setdownlimit";
|
||||
return axios
|
||||
.patch(
|
||||
`${
|
||||
config.serverAddr
|
||||
}/fileinfo?act=${act}&shareid=${shareId}&downlimit=${downLimit}`
|
||||
)
|
||||
.then(response => response.data.Code === 200);
|
||||
};
|
||||
|
||||
export const addLocalFiles = () => {
|
||||
const act = "addlocalfiles";
|
||||
return axios
|
||||
.patch(`${config.serverAddr}/fileinfo?act=${act}`)
|
||||
.then(response => response.data.Code === 200);
|
||||
};
|
202
client/libs/api_upload.js
Normal file
202
client/libs/api_upload.js
Normal file
|
@ -0,0 +1,202 @@
|
|||
import axios from "axios";
|
||||
import { config } from "../config";
|
||||
import { makePostBody } from "./utils";
|
||||
|
||||
const wait = 5000; // TODO: should tune according to backend
|
||||
const retryMax = 100000;
|
||||
const maxUploadLen = 20 * 1024 * 1024;
|
||||
|
||||
// TODO: add to react-intl
|
||||
const msgUploadFailed = "Fail to upload, upload is stopped.";
|
||||
const msgUploadFailedAndRetry = "Fail to upload, retrying...";
|
||||
const msgFileExists = "File exists.";
|
||||
const msgTooBigChunk = "Too big chunk.";
|
||||
const msgFileNotFound = "File not found, upload stopped.";
|
||||
|
||||
function randomWait() {
|
||||
return Math.random() * wait;
|
||||
}
|
||||
|
||||
function isKnownErr(res) {
|
||||
return res != null && res.Code != null && res.Msg != null;
|
||||
}
|
||||
|
||||
export class FileUploader {
|
||||
constructor(onStart, onProgress, onFinish, onError) {
|
||||
this.onStart = onStart;
|
||||
this.onProgress = onProgress;
|
||||
this.onFinish = onFinish;
|
||||
this.onError = onError;
|
||||
this.retry = retryMax;
|
||||
this.reader = new FileReader();
|
||||
|
||||
this.uploadFile = file => {
|
||||
return this.startUpload(file);
|
||||
};
|
||||
|
||||
this.startUpload = file => {
|
||||
return axios
|
||||
.post(
|
||||
`${config.serverAddr}/startupload`,
|
||||
makePostBody({
|
||||
fname: file.name
|
||||
})
|
||||
)
|
||||
.then(response => {
|
||||
if (
|
||||
response.data.ShareId == null ||
|
||||
response.data.Start === null ||
|
||||
response.data.Length === null
|
||||
) {
|
||||
throw response;
|
||||
} else {
|
||||
this.onStart(response.data.ShareId, file.name);
|
||||
return this.upload(
|
||||
{
|
||||
shareId: response.data.ShareId,
|
||||
start: response.data.Start,
|
||||
length: response.data.Length
|
||||
},
|
||||
file
|
||||
);
|
||||
}
|
||||
})
|
||||
.catch(response => {
|
||||
// TODO: this is not good because error may not be response
|
||||
if (isKnownErr(response.data) && response.data.Code === 429) {
|
||||
setTimeout(this.startUpload, randomWait(), file);
|
||||
} else if (isKnownErr(response.data) && response.data.Code === 412) {
|
||||
this.onError(msgFileExists);
|
||||
} else if (isKnownErr(response.data) && response.data.Code === 404) {
|
||||
this.onError(msgFileNotFound);
|
||||
} else if (this.retry > 0) {
|
||||
this.retry--;
|
||||
this.onError(msgUploadFailedAndRetry);
|
||||
console.trace(response);
|
||||
setTimeout(this.startUpload, randomWait(), file);
|
||||
} else {
|
||||
this.onError(msgUploadFailed);
|
||||
console.trace(response);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
this.prepareReader = (shareInfo, end, resolve, reject) => {
|
||||
this.reader.onerror = err => {
|
||||
reject(err);
|
||||
};
|
||||
|
||||
this.reader.onloadend = event => {
|
||||
const formData = new FormData();
|
||||
formData.append("shareid", shareInfo.shareId);
|
||||
formData.append("start", shareInfo.start);
|
||||
formData.append("len", end - shareInfo.start);
|
||||
formData.append("chunk", new Blob([event.target.result]));
|
||||
|
||||
const url = `${config.serverAddr}/upload`;
|
||||
const headers = {
|
||||
"Content-Type": "multipart/form-data"
|
||||
};
|
||||
|
||||
try {
|
||||
axios
|
||||
.post(url, formData, { headers })
|
||||
.then(response => resolve(response))
|
||||
.catch(err => {
|
||||
reject(err);
|
||||
});
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
this.upload = (shareInfo, file) => {
|
||||
const uploaded = shareInfo.start + shareInfo.length;
|
||||
const end = uploaded < file.size ? uploaded : file.size;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (
|
||||
end == null ||
|
||||
shareInfo.start == null ||
|
||||
end - shareInfo.start >= maxUploadLen
|
||||
) {
|
||||
throw new Error(msgTooBigChunk);
|
||||
}
|
||||
|
||||
const chunk = file.slice(shareInfo.start, end);
|
||||
this.prepareReader(shareInfo, end, resolve, reject);
|
||||
this.reader.readAsArrayBuffer(chunk);
|
||||
})
|
||||
.then(response => {
|
||||
if (
|
||||
response.data.ShareId == null ||
|
||||
response.data.Start == null ||
|
||||
response.data.Length == null ||
|
||||
response.data.Start !== end
|
||||
) {
|
||||
throw response;
|
||||
} else {
|
||||
if (end < file.size) {
|
||||
this.onProgress(shareInfo.shareId, end / file.size);
|
||||
return this.upload(
|
||||
{
|
||||
shareId: shareInfo.shareId,
|
||||
start: shareInfo.start + shareInfo.length,
|
||||
length: shareInfo.length
|
||||
},
|
||||
file
|
||||
);
|
||||
} else {
|
||||
return this.finishUpload(shareInfo);
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(response => {
|
||||
// possible error: response.data.Start == null || response.data.Start !== end
|
||||
if (isKnownErr(response.data) && response.data.Code === 429) {
|
||||
setTimeout(this.upload, randomWait(), shareInfo, file);
|
||||
} else if (isKnownErr(response.data) && response.data.Code === 404) {
|
||||
this.onError(msgFileNotFound);
|
||||
} else if (this.retry > 0) {
|
||||
this.retry--;
|
||||
setTimeout(this.upload, randomWait(), shareInfo, file);
|
||||
this.onError(msgUploadFailedAndRetry);
|
||||
console.trace(response);
|
||||
} else {
|
||||
this.onError(msgUploadFailed);
|
||||
console.trace(response);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
this.finishUpload = shareInfo => {
|
||||
return axios
|
||||
.post(`${config.serverAddr}/finishupload?shareid=${shareInfo.shareId}`)
|
||||
.then(response => {
|
||||
// TODO: should check Code instead of Url
|
||||
if (response.data.ShareId != null && response.data.Start == null) {
|
||||
this.onFinish();
|
||||
return response.data.ShareId;
|
||||
} else {
|
||||
throw response;
|
||||
}
|
||||
})
|
||||
.catch(response => {
|
||||
if (isKnownErr(response.data) && response.data.Code === 429) {
|
||||
setTimeout(this.finishUpload, randomWait(), shareInfo);
|
||||
} else if (isKnownErr(response.data) && response.data.Code === 404) {
|
||||
this.onError(msgFileNotFound);
|
||||
} else if (this.retry > 0) {
|
||||
this.retry--;
|
||||
setTimeout(this.finishUpload, randomWait(), shareInfo);
|
||||
this.onError(msgUploadFailedAndRetry);
|
||||
console.trace(response);
|
||||
} else {
|
||||
this.onError(msgUploadFailed);
|
||||
console.trace(response);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
18
client/libs/file_type.js
Normal file
18
client/libs/file_type.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
const fileTypeMap = {
|
||||
jpg: "image",
|
||||
jpeg: "image",
|
||||
png: "image",
|
||||
bmp: "image",
|
||||
gz: "archive",
|
||||
mov: "video",
|
||||
mp4: "video",
|
||||
mov: "video",
|
||||
avi: "video"
|
||||
};
|
||||
|
||||
export const getFileExt = fileName => fileName.split(".").pop();
|
||||
|
||||
export const getFileType = fileName => {
|
||||
const ext = getFileExt(fileName);
|
||||
return fileTypeMap[ext] != null ? fileTypeMap[ext] : "file";
|
||||
};
|
34
client/libs/test/api_auth_test.js
Normal file
34
client/libs/test/api_auth_test.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { login, logout } from "../api_auth";
|
||||
import { config } from "../../config";
|
||||
|
||||
const serverAddr = config.serverAddr;
|
||||
const testId = config.testId;
|
||||
const testPwd = config.testPwd;
|
||||
|
||||
export function testAuth() {
|
||||
return testLogin()
|
||||
.then(testLogout)
|
||||
.catch(err => {
|
||||
console.error("auth: fail", err);
|
||||
});
|
||||
}
|
||||
|
||||
export function testLogin() {
|
||||
return login(serverAddr, testId, testPwd).then(ok => {
|
||||
if (ok === true) {
|
||||
console.log("login api: ok");
|
||||
} else {
|
||||
throw new Error("login api: failed");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function testLogout() {
|
||||
return logout(serverAddr).then(ok => {
|
||||
if (ok === true) {
|
||||
console.log("logout api: ok");
|
||||
} else {
|
||||
throw new Error("logout api: failed");
|
||||
}
|
||||
});
|
||||
}
|
167
client/libs/test/api_share_test.js
Normal file
167
client/libs/test/api_share_test.js
Normal file
|
@ -0,0 +1,167 @@
|
|||
import { FileUploader } from "../api_upload";
|
||||
import {
|
||||
del,
|
||||
list,
|
||||
shadowId,
|
||||
publishId,
|
||||
setDownLimit,
|
||||
addLocalFiles
|
||||
} from "../api_share";
|
||||
import { testLogin, testLogout } from "./api_auth_test";
|
||||
|
||||
const fileName = "filename";
|
||||
|
||||
function upload(fileName) {
|
||||
return new Promise(resolve => {
|
||||
const onStart = () => true;
|
||||
const onProgress = () => true;
|
||||
const onFinish = () => resolve();
|
||||
const onError = err => {
|
||||
throw new Error(JSON.stringify(err));
|
||||
};
|
||||
const file = new File(["foo"], fileName, {
|
||||
type: "text/plain"
|
||||
});
|
||||
|
||||
const uploader = new FileUploader(onStart, onProgress, onFinish, onError);
|
||||
uploader.uploadFile(file);
|
||||
});
|
||||
}
|
||||
|
||||
function getIdFromList(list, fileName) {
|
||||
if (list == null) {
|
||||
throw new Error("list: list fail");
|
||||
}
|
||||
|
||||
// TODO: should verify file name
|
||||
const filterInfo = list.find(info => {
|
||||
return info.PathLocal.includes(fileName);
|
||||
});
|
||||
|
||||
if (filterInfo == null) {
|
||||
console.error(list);
|
||||
throw new Error("list: file name not found");
|
||||
} else {
|
||||
return filterInfo.Id;
|
||||
}
|
||||
}
|
||||
|
||||
function delWithName(fileName) {
|
||||
return list().then(infoList => {
|
||||
const infoToDel = infoList.find(info => {
|
||||
return info.PathLocal.includes(fileName);
|
||||
});
|
||||
|
||||
if (infoToDel == null) {
|
||||
console.warn("delWithName: name not found");
|
||||
} else {
|
||||
return del(infoToDel.Id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function testShadowPublishId() {
|
||||
return testLogin()
|
||||
.then(() => upload(fileName))
|
||||
.then(list)
|
||||
.then(infoList => {
|
||||
return getIdFromList(infoList, fileName);
|
||||
})
|
||||
.then(shareId => {
|
||||
return shadowId(shareId).then(secretId => {
|
||||
if (shareId === secretId) {
|
||||
throw new Error("shadowId: id not changed");
|
||||
} else {
|
||||
return secretId;
|
||||
}
|
||||
});
|
||||
})
|
||||
.then(secretId => {
|
||||
return list().then(infoList => {
|
||||
const info = infoList.find(info => {
|
||||
return info.Id === secretId;
|
||||
});
|
||||
|
||||
if (info.PathLocal.includes(fileName)) {
|
||||
console.log("shadowId api: ok", secretId);
|
||||
return secretId;
|
||||
} else {
|
||||
throw new Error("shadowId pai: file not found", infoList, fileName);
|
||||
}
|
||||
});
|
||||
})
|
||||
.then(secretId => {
|
||||
return publishId(secretId).then(publicId => {
|
||||
if (publicId === secretId) {
|
||||
// TODO: it is not enough to check they are not equal
|
||||
throw new Error("publicId: id not changed");
|
||||
} else {
|
||||
console.log("publishId api: ok", publicId);
|
||||
return publicId;
|
||||
}
|
||||
});
|
||||
})
|
||||
.then(shareId => del(shareId))
|
||||
.then(testLogout)
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
delWithName(fileName);
|
||||
});
|
||||
}
|
||||
|
||||
export function testSetDownLimit() {
|
||||
const downLimit = 777;
|
||||
|
||||
return testLogin()
|
||||
.then(() => upload(fileName))
|
||||
.then(list)
|
||||
.then(infoList => {
|
||||
return getIdFromList(infoList, fileName);
|
||||
})
|
||||
.then(shareId => {
|
||||
return setDownLimit(shareId, downLimit).then(ok => {
|
||||
if (!ok) {
|
||||
throw new Error("setDownLimit: failed");
|
||||
} else {
|
||||
return shareId;
|
||||
}
|
||||
});
|
||||
})
|
||||
.then(shareId => {
|
||||
return list().then(infoList => {
|
||||
const info = infoList.find(info => {
|
||||
return info.Id == shareId;
|
||||
});
|
||||
|
||||
if (info.DownLimit === downLimit) {
|
||||
console.log("setDownLimit api: ok");
|
||||
return shareId;
|
||||
} else {
|
||||
throw new Error("setDownLimit api: limit unchanged");
|
||||
}
|
||||
});
|
||||
})
|
||||
.then(shareId => del(shareId))
|
||||
.then(testLogout)
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
delWithName(fileName);
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: need to add local file and test
|
||||
export function testAddLocalFiles() {
|
||||
return testLogin()
|
||||
.then(() => addLocalFiles())
|
||||
.then(ok => {
|
||||
if (ok) {
|
||||
console.log("addLocalFiles api: ok");
|
||||
} else {
|
||||
throw new Error("addLocalFiles api: failed");
|
||||
}
|
||||
})
|
||||
.then(() => testLogout())
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
});
|
||||
}
|
25
client/libs/test/api_test.js
Normal file
25
client/libs/test/api_test.js
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { testAuth } from "./api_auth_test";
|
||||
import { testUploadOneFile } from "./api_upload_test";
|
||||
import {
|
||||
testAddLocalFiles,
|
||||
testSetDownLimit,
|
||||
testShadowPublishId
|
||||
} from "./api_share_test";
|
||||
import { testUpDownBatch } from "./api_up_down_batch_test";
|
||||
|
||||
console.log("Test started");
|
||||
|
||||
const fileName = `test_filename${Date.now()}`;
|
||||
const file = new File(["foo"], fileName, {
|
||||
type: "text/plain"
|
||||
});
|
||||
|
||||
testAuth()
|
||||
.then(testShadowPublishId)
|
||||
.then(() => testUploadOneFile(file, fileName))
|
||||
.then(testSetDownLimit)
|
||||
.then(testAddLocalFiles)
|
||||
.then(testUpDownBatch)
|
||||
.then(() => {
|
||||
console.log("Tests are finished");
|
||||
});
|
97
client/libs/test/api_up_down_batch_test.js
Normal file
97
client/libs/test/api_up_down_batch_test.js
Normal file
|
@ -0,0 +1,97 @@
|
|||
import axios from "axios";
|
||||
import md5 from "md5";
|
||||
|
||||
import { config } from "../../config";
|
||||
import { testUpload } from "./api_upload_test";
|
||||
import { list, del } from "../api_share";
|
||||
import { testLogin, testLogout } from "./api_auth_test";
|
||||
|
||||
export function testUpDownBatch() {
|
||||
const fileInfos = [
|
||||
{
|
||||
fileName: "test_2MB_1",
|
||||
content: new Array(1024 * 1024 * 2).join("x")
|
||||
},
|
||||
{
|
||||
fileName: "test_1MB_1",
|
||||
content: new Array(1024 * 1024 * 1).join("x")
|
||||
},
|
||||
{
|
||||
fileName: "test_2MB_2",
|
||||
content: new Array(1024 * 1024 * 2).join("x")
|
||||
},
|
||||
{
|
||||
fileName: "test_1B",
|
||||
content: `${new Array(3).join("o")}${new Array(3).join("x")}`
|
||||
}
|
||||
];
|
||||
|
||||
return testLogin()
|
||||
.then(() => {
|
||||
const promises = fileInfos.map(info => {
|
||||
const file = new File([info.content], info.fileName, {
|
||||
type: "text/plain"
|
||||
});
|
||||
|
||||
return testUpAndDownOneFile(file, info.fileName);
|
||||
});
|
||||
|
||||
return Promise.all(promises);
|
||||
})
|
||||
.then(() => {
|
||||
testLogout();
|
||||
})
|
||||
.catch(err => console.error(err));
|
||||
}
|
||||
|
||||
export function testUpAndDownOneFile(file, fileName) {
|
||||
return delTestFile(fileName)
|
||||
.then(() => testUpload(file))
|
||||
.then(shareId => testDownload(shareId, file))
|
||||
.catch(err => console.error(err));
|
||||
}
|
||||
|
||||
function delTestFile(fileName) {
|
||||
return list().then(infos => {
|
||||
const info = infos.find(info => {
|
||||
return info.PathLocal === fileName;
|
||||
});
|
||||
|
||||
if (info == null) {
|
||||
console.log("up-down: file not found", fileName);
|
||||
} else {
|
||||
return del(info.Id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function testDownload(shareId, file) {
|
||||
return axios
|
||||
.get(`${config.serverAddr}/download?shareid=${shareId}`)
|
||||
.then(response => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = event => {
|
||||
const upHash = md5(event.target.result);
|
||||
const downHash = md5(response.data);
|
||||
if (upHash !== downHash) {
|
||||
console.error(
|
||||
"up&down: hash unmatch",
|
||||
file.name,
|
||||
upHash,
|
||||
downHash,
|
||||
upHash.length,
|
||||
downHash.length
|
||||
);
|
||||
} else {
|
||||
console.log("up&down: ok: hash match", file.name, upHash, downHash);
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
|
||||
reader.onerror = err => reject(err);
|
||||
|
||||
reader.readAsText(file);
|
||||
});
|
||||
});
|
||||
}
|
63
client/libs/test/api_upload_test.js
Normal file
63
client/libs/test/api_upload_test.js
Normal file
|
@ -0,0 +1,63 @@
|
|||
import { FileUploader } from "../api_upload";
|
||||
import { list, del } from "../api_share";
|
||||
import { testLogin, testLogout } from "./api_auth_test";
|
||||
|
||||
function verify(fileName) {
|
||||
return list()
|
||||
.then(list => {
|
||||
if (list == null) {
|
||||
throw new Error("upload: list fail");
|
||||
}
|
||||
|
||||
// TODO: should verify file name
|
||||
const filterInfo = list.find(info => {
|
||||
return info.PathLocal.includes(fileName);
|
||||
});
|
||||
|
||||
if (filterInfo == null) {
|
||||
console.error(list);
|
||||
throw new Error("upload: file name not found");
|
||||
} else {
|
||||
return filterInfo.Id;
|
||||
}
|
||||
})
|
||||
.then(shareId => {
|
||||
console.log("upload api: ok");
|
||||
del(shareId);
|
||||
})
|
||||
.then(testLogout)
|
||||
.catch(err => {
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
export function testUpload(file) {
|
||||
const onStart = () => true;
|
||||
const onProgress = () => true;
|
||||
const onFinish = () => true;
|
||||
const onError = err => {
|
||||
throw new Error(JSON.stringify(err));
|
||||
};
|
||||
const uploader = new FileUploader(onStart, onProgress, onFinish, onError);
|
||||
|
||||
return uploader.uploadFile(file).catch(err => {
|
||||
console.error(err);
|
||||
});
|
||||
}
|
||||
|
||||
export function testUploadOneFile(file, fileName) {
|
||||
const onStart = () => true;
|
||||
const onProgress = () => true;
|
||||
const onFinish = () => true;
|
||||
const onError = err => {
|
||||
throw new Error(JSON.stringify(err));
|
||||
};
|
||||
const uploader = new FileUploader(onStart, onProgress, onFinish, onError);
|
||||
|
||||
return testLogin()
|
||||
.then(() => uploader.uploadFile(file))
|
||||
.then(() => verify(fileName))
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
});
|
||||
}
|
5
client/libs/utils.js
Normal file
5
client/libs/utils.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
export function makePostBody(paramMap) {
|
||||
return Object.keys(paramMap)
|
||||
.map(key => `${key}=${paramMap[key]}`)
|
||||
.join("&");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue