fix(fe): update query params and refactor initAll

This commit is contained in:
hexxa 2022-01-22 10:31:34 +08:00 committed by Hexxa
parent 8165c70bcb
commit f06c5f5a7d
6 changed files with 157 additions and 85 deletions

View file

@ -10,9 +10,10 @@ import {
GetSharingDirResp, GetSharingDirResp,
} from "./"; } from "./";
const filePathQuery = "fp"; export const filePathQuery = "fp";
const listDirQuery = "dp"; export const listDirQuery = "dp";
const shareIDQuery = "sh"; export const shareIDQuery = "shid";
export const shareDirQuery = "shdir";
// TODO: get timeout from server // TODO: get timeout from server
function translateResp(resp: Response<any>): Response<any> { function translateResp(resp: Response<any>): Response<any> {

View file

@ -10,6 +10,7 @@ import {
} from "./"; } from "./";
import { makePromise } from "../test/helpers"; import { makePromise } from "../test/helpers";
export interface FilesClientResps { export interface FilesClientResps {
createMockRespID?: number; createMockRespID?: number;
createMockResp?: Response; createMockResp?: Response;
@ -271,47 +272,47 @@ export class JestFilesClient {
this.url = url; this.url = url;
} }
create = jest.fn().mockReturnValueOnce(makePromise(resps.createMockResp)); create = jest.fn().mockReturnValue(makePromise(resps.createMockResp));
delete = jest.fn().mockReturnValueOnce(makePromise(resps.deleteMockResp)); delete = jest.fn().mockReturnValue(makePromise(resps.deleteMockResp));
metadata = jest.fn().mockReturnValueOnce(makePromise(resps.metadataMockResp)); metadata = jest.fn().mockReturnValue(makePromise(resps.metadataMockResp));
mkdir = jest.fn().mockReturnValueOnce(makePromise(resps.mkdirMockResp)); mkdir = jest.fn().mockReturnValue(makePromise(resps.mkdirMockResp));
move = jest.fn().mockReturnValueOnce(makePromise(resps.moveMockResp)); move = jest.fn().mockReturnValue(makePromise(resps.moveMockResp));
uploadChunk = jest uploadChunk = jest
.fn() .fn()
.mockReturnValueOnce(makePromise(resps.uploadChunkMockResp)); .mockReturnValue(makePromise(resps.uploadChunkMockResp));
uploadStatus = jest uploadStatus = jest
.fn() .fn()
.mockReturnValueOnce(makePromise(resps.uploadStatusMockResp)); .mockReturnValue(makePromise(resps.uploadStatusMockResp));
list = jest.fn().mockReturnValueOnce(makePromise(resps.listMockResp)); list = jest.fn().mockReturnValue(makePromise(resps.listMockResp));
listHome = jest.fn().mockReturnValueOnce(makePromise(resps.listHomeMockResp)); listHome = jest.fn().mockReturnValue(makePromise(resps.listHomeMockResp));
listUploadings = jest listUploadings = jest
.fn() .fn()
.mockReturnValueOnce(makePromise(resps.listUploadingsMockResp)); .mockReturnValue(makePromise(resps.listUploadingsMockResp));
deleteUploading = jest deleteUploading = jest
.fn() .fn()
.mockReturnValueOnce(makePromise(resps.deleteUploadingMockResp)); .mockReturnValue(makePromise(resps.deleteUploadingMockResp));
addSharing = jest addSharing = jest
.fn() .fn()
.mockReturnValueOnce(makePromise(resps.addSharingMockResp)); .mockReturnValue(makePromise(resps.addSharingMockResp));
deleteSharing = jest deleteSharing = jest
.fn() .fn()
.mockReturnValueOnce(makePromise(resps.deleteSharingMockResp)); .mockReturnValue(makePromise(resps.deleteSharingMockResp));
isSharing = jest isSharing = jest
.fn() .fn()
.mockReturnValueOnce(makePromise(resps.isSharingMockResp)); .mockReturnValue(makePromise(resps.isSharingMockResp));
listSharings = jest listSharings = jest
.fn() .fn()
.mockReturnValueOnce(makePromise(resps.listSharingsMockResp)); .mockReturnValue(makePromise(resps.listSharingsMockResp));
listSharingIDs = jest listSharingIDs = jest
.fn() .fn()
.mockReturnValueOnce(makePromise(resps.listSharingIDsMockResp)); .mockReturnValue(makePromise(resps.listSharingIDsMockResp));
getSharingDir = jest getSharingDir = jest
.fn() .fn()
.mockReturnValueOnce(makePromise(resps.getSharingDirMockResp)); .mockReturnValue(makePromise(resps.getSharingDirMockResp));
generateHash = jest generateHash = jest
.fn() .fn()
.mockReturnValueOnce(makePromise(resps.generateHashMockResp)); .mockReturnValue(makePromise(resps.generateHashMockResp));
download = jest.fn().mockReturnValueOnce(makePromise(resps.downloadMockResp)); download = jest.fn().mockReturnValue(makePromise(resps.downloadMockResp));
} }
export const NewMockFilesClient = (url: string): IFilesClient => { export const NewMockFilesClient = (url: string): IFilesClient => {

View file

@ -75,16 +75,16 @@ export class JestSettingsClient {
this.url = url; this.url = url;
} }
health = jest.fn().mockReturnValueOnce(makePromise(resps.healthMockResp)); health = jest.fn().mockReturnValue(makePromise(resps.healthMockResp));
getClientCfg = jest getClientCfg = jest
.fn() .fn()
.mockReturnValueOnce(makePromise(resps.getClientCfgMockResp)); .mockReturnValue(makePromise(resps.getClientCfgMockResp));
setClientCfg = jest setClientCfg = jest
.fn() .fn()
.mockReturnValueOnce(makePromise(resps.setClientCfgMockResp)); .mockReturnValue(makePromise(resps.setClientCfgMockResp));
reportErrors = jest reportErrors = jest
.fn() .fn()
.mockReturnValueOnce(makePromise(resps.reportErrorsMockResp)); .mockReturnValue(makePromise(resps.reportErrorsMockResp));
} }
export const NewMockSettingsClient = (url: string): ISettingsClient => { export const NewMockSettingsClient = (url: string): ISettingsClient => {

View file

@ -14,6 +14,7 @@ import { LoginProps } from "./pane_login";
import { Flexbox } from "./layout/flexbox"; import { Flexbox } from "./layout/flexbox";
import { Container } from "./layout/container"; import { Container } from "./layout/container";
import { Rows, Row } from "./layout/rows"; import { Rows, Row } from "./layout/rows";
import { shareIDQuery } from "../client/files";
export interface SharingsProps { export interface SharingsProps {
sharings: Map<string, string>; sharings: Map<string, string>;
@ -63,7 +64,7 @@ export class SharingsPanel extends React.Component<Props, State, {}> {
const shareID = sharings.get(dirPath); const shareID = sharings.get(dirPath);
const sharingURL = `${ const sharingURL = `${
document.location.href.split("?")[0] document.location.href.split("?")[0]
}?sh=${shareID}`; }?${shareIDQuery}=${shareID}`;
const row1 = ( const row1 = (
<div> <div>

View file

@ -14,12 +14,13 @@ import {
Quota, Quota,
Response, Response,
roleVisitor, roleVisitor,
roleUser,
roleAdmin, roleAdmin,
visitorID, visitorID,
ClientConfig, ClientConfig,
Preferences, Preferences,
} from "../client"; } from "../client";
import { FilesClient } from "../client/files"; import { FilesClient, shareIDQuery, shareDirQuery } from "../client/files";
import { UsersClient } from "../client/users"; import { UsersClient } from "../client/users";
import { SettingsClient } from "../client/settings"; import { SettingsClient } from "../client/settings";
import { UploadEntry, UploadState } from "../worker/interface"; import { UploadEntry, UploadState } from "../worker/interface";
@ -406,11 +407,75 @@ export class Updater {
this.props.ui.control.options = newOptions.merge(leftOpts); this.props.ui.control.options = newOptions.merge(leftOpts);
}; };
initStateForVisitor = async (): Promise<any> => { initClientCfg = async (): Promise<string> => {
const statuses = await Promise.all([this.getClientCfg()]); const status = await this.getClientCfg();
if (status !== "") {
return status;
}
return await this.syncLan();
};
initCwd = async (shareDir: string): Promise<string> => {
if (shareDir !== "") {
// in sharing mode
const dirPath = List(shareDir.split("/"));
this.props.ui.control.controls = this.props.ui.control.controls.set(
sharingCtrl,
ctrlOn
);
this.props.filesInfo.dirPath = dirPath;
} else {
this.props.ui.control.controls = this.props.ui.control.controls.set(
sharingCtrl,
ctrlOff
);
this.props.filesInfo.dirPath = List([]);
}
return "";
};
initFiles = async (shareDir: string): Promise<string> => {
let status = await this.initCwd(shareDir);
if (status !== "") {
return status;
}
status = await this.syncCwd();
if (status !== "") {
return status;
}
return await this.syncIsSharing(this.props.filesInfo.dirPath.join("/"));
};
initUploadings = async (): Promise<string> => {
const status = await this.refreshUploadings();
if (status !== "") {
return status;
}
this.initUploads();
return "";
};
initSharings = async (): Promise<string> => {
return await this.listSharings();
};
initAdmin = async (): Promise<string> => {
const statuses = await Promise.all([this.listRoles(), this.listUsers()]);
if (statuses.join("") !== "") { if (statuses.join("") !== "") {
return statuses.join(";"); return statuses.join(";");
} }
return "";
};
initStateForVisitor = async (): Promise<any> => {
const status = await this.getClientCfg();
if (status !== "") {
return status;
}
const syncLanStatus = await this.syncLan(); const syncLanStatus = await this.syncLan();
if (syncLanStatus !== "") { if (syncLanStatus !== "") {
@ -458,59 +523,40 @@ export class Updater {
return ""; return "";
}; };
getParamMap = async ( initParams = async (
params: URLSearchParams params: URLSearchParams
): Promise<Map<string, string>> => { ): Promise<Map<string, string>> => {
let paramMap = Map<string, string>(); let paramMap = Map<string, string>();
paramMap = paramMap.set("sh", ""); const paramKeys = [shareIDQuery, shareDirQuery];
paramMap = paramMap.set("dir", ""); paramKeys.forEach((key) => {
const val = params.get(key);
paramMap = paramMap.set(key, val != null ? val : "");
});
let shareID = params.get("sh"); const shareID = paramMap.get(shareIDQuery);
if (shareID != null && shareID !== "") { if (shareID !== "") {
paramMap = paramMap.set("sh", shareID);
const resp = await this.filesClient.getSharingDir(shareID); const resp = await this.filesClient.getSharingDir(shareID);
if (resp.status === 200) { if (resp.status === 200) {
paramMap = paramMap.set("dir", resp.data.sharingDir); paramMap = paramMap.set(shareDirQuery, resp.data.sharingDir);
}
} else { } else {
paramMap = paramMap.set("dir", params.get("dir")); ErrorLogger().error(
`initParams: unexpected response ${resp.status} ${resp.data}`
);
}
} }
return paramMap; return paramMap;
}; };
initCwd = async (params: URLSearchParams): Promise<string> => { initAuth = async (): Promise<string> => {
const paramMap = await this.getParamMap(params);
const dir = paramMap.get("dir", "");
if (dir != null && dir !== "") {
// in sharing mode
const dirPath = List(dir.split("/"));
this.props.ui.control.controls = this.props.ui.control.controls.set(
sharingCtrl,
ctrlOn
);
this.props.filesInfo.dirPath = dirPath;
} else {
this.props.ui.control.controls = this.props.ui.control.controls.set(
sharingCtrl,
ctrlOff
);
this.props.filesInfo.dirPath = List([]);
}
return "";
};
initAll = async (params: URLSearchParams): Promise<string> => {
const isAuthedStatus = await this.syncIsAuthed(); const isAuthedStatus = await this.syncIsAuthed();
if (isAuthedStatus !== "") { if (isAuthedStatus !== "") {
return isAuthedStatus; return isAuthedStatus;
} }
const selfStatuses = await Promise.all([this.self(), this.initCwd(params)]); const selfStatuses = await this.self();
if (selfStatuses.join("") !== "") { if (selfStatuses !== "") {
return selfStatuses.join(";"); return selfStatuses;
} }
const getCapStatus = await this.getCaptchaID(); const getCapStatus = await this.getCaptchaID();
@ -518,35 +564,58 @@ export class Updater {
return getCapStatus; return getCapStatus;
} }
return "";
};
initAll = async (params: URLSearchParams): Promise<string> => {
const paramMap = await this.initParams(params);
const authStatus = await this.initAuth();
if (authStatus !== "") {
return authStatus;
}
const initClientCfgStatus = await this.initClientCfg();
if (initClientCfgStatus !== "") {
return initClientCfgStatus;
}
this.initUITree(); this.initUITree();
const isInSharingMode = this.props.ui.control.controls.get(sharingCtrl); const isInSharingMode = this.props.ui.control.controls.get(sharingCtrl);
if ( if (
this.props.login.userRole === roleVisitor && (this.props.login.userRole === roleVisitor &&
isInSharingMode !== ctrlOn isInSharingMode !== ctrlOn) ||
this.props.login.userRole === roleUser ||
this.props.login.userRole === roleAdmin
) { ) {
return this.initStateForVisitor(); const shareDir = paramMap.get(shareDirQuery);
const initFilesStatus = await this.initFiles(shareDir);
if (initFilesStatus !== "") {
return initFilesStatus;
}
} }
const cwdStatus = await this.syncCwd(); if (
if (cwdStatus !== "") { this.props.login.userRole === roleUser ||
return cwdStatus; this.props.login.userRole === roleAdmin
) {
const statuses = await Promise.all([
this.initUploadings(),
this.initSharings(),
]);
if (statuses.join("") !== "") {
return statuses.join(";");
} }
const isSharingStatus = await this.syncIsSharing(
this.props.filesInfo.dirPath.join("/")
);
if (isSharingStatus !== "") {
return isSharingStatus;
} }
if (this.props.login.userRole === roleAdmin) { if (this.props.login.userRole === roleAdmin) {
return this.initStateForAdmin(); const status = await this.initAdmin();
} else if (this.props.login.userRole === roleVisitor) { if (status !== "") {
// visitor under sharing mode return status;
return this.initStateForVisitor();
} }
return this.initStateForAuthedUser(); }
return "";
}; };
resetUser = () => { resetUser = () => {

View file

@ -28,7 +28,7 @@ var (
// queries // queries
FilePathQuery = "fp" FilePathQuery = "fp"
ListDirQuery = "dp" ListDirQuery = "dp"
ShareIDQuery = "sh" ShareIDQuery = "shid"
// headers // headers
rangeHeader = "Range" rangeHeader = "Range"