feat(fe,be): enable short sharing urls, enable file info migration
This commit is contained in:
parent
18ee149956
commit
6dc8802752
5 changed files with 145 additions and 27 deletions
|
@ -7,10 +7,12 @@ import {
|
||||||
ListUploadingsResp,
|
ListUploadingsResp,
|
||||||
ListSharingsResp,
|
ListSharingsResp,
|
||||||
ListSharingIDsResp,
|
ListSharingIDsResp,
|
||||||
|
GetSharingDirResp,
|
||||||
} from "./";
|
} from "./";
|
||||||
|
|
||||||
const filePathQuery = "fp";
|
const filePathQuery = "fp";
|
||||||
const listDirQuery = "dp";
|
const listDirQuery = "dp";
|
||||||
|
const shareIDQuery = "sh";
|
||||||
// TODO: get timeout from server
|
// TODO: get timeout from server
|
||||||
|
|
||||||
function translateResp(resp: Response<any>): Response<any> {
|
function translateResp(resp: Response<any>): Response<any> {
|
||||||
|
@ -208,6 +210,16 @@ export class FilesClient extends BaseClient {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
getSharingDir = (shareID: string): Promise<Response<GetSharingDirResp>> => {
|
||||||
|
return this.do({
|
||||||
|
method: "get",
|
||||||
|
url: `${this.url}/v1/fs/sharings/dirs`,
|
||||||
|
params: {
|
||||||
|
[shareIDQuery]: shareID,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
generateHash = (filePath: string): Promise<Response> => {
|
generateHash = (filePath: string): Promise<Response> => {
|
||||||
return this.do({
|
return this.do({
|
||||||
method: "post",
|
method: "post",
|
||||||
|
|
|
@ -5,6 +5,7 @@ import {
|
||||||
ListUploadingsResp,
|
ListUploadingsResp,
|
||||||
ListSharingsResp,
|
ListSharingsResp,
|
||||||
ListSharingIDsResp,
|
ListSharingIDsResp,
|
||||||
|
GetSharingDirResp,
|
||||||
} from "./";
|
} from "./";
|
||||||
|
|
||||||
export interface FilesClientResps {
|
export interface FilesClientResps {
|
||||||
|
@ -26,6 +27,7 @@ export interface FilesClientResps {
|
||||||
deleteSharingMockResp?: Response;
|
deleteSharingMockResp?: Response;
|
||||||
listSharingsMockResp?: Response<ListSharingsResp>;
|
listSharingsMockResp?: Response<ListSharingsResp>;
|
||||||
listSharingIDsMockResp?: Response<ListSharingIDsResp>;
|
listSharingIDsMockResp?: Response<ListSharingIDsResp>;
|
||||||
|
getSharingDirMockResp?: Response<GetSharingDirResp>;
|
||||||
isSharingMockResp?: Response;
|
isSharingMockResp?: Response;
|
||||||
generateHashMockResp?: Response;
|
generateHashMockResp?: Response;
|
||||||
downloadMockResp: Response;
|
downloadMockResp: Response;
|
||||||
|
@ -144,6 +146,13 @@ export const resps = {
|
||||||
IDs: sharingIDs,
|
IDs: sharingIDs,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
getSharingDirMockResp: {
|
||||||
|
status: 200,
|
||||||
|
statusText: "",
|
||||||
|
data: {
|
||||||
|
sharingDir: "/admin/sharing",
|
||||||
|
},
|
||||||
|
},
|
||||||
isSharingMockResp: { status: 200, statusText: "", data: {} },
|
isSharingMockResp: { status: 200, statusText: "", data: {} },
|
||||||
generateHashMockResp: { status: 200, statusText: "", data: {} },
|
generateHashMockResp: { status: 200, statusText: "", data: {} },
|
||||||
downloadMockResp: {
|
downloadMockResp: {
|
||||||
|
@ -231,10 +240,15 @@ export class MockFilesClient {
|
||||||
listSharings = (): Promise<Response<ListSharingsResp>> => {
|
listSharings = (): Promise<Response<ListSharingsResp>> => {
|
||||||
return this.wrapPromise(this.resps.listSharingsMockResp);
|
return this.wrapPromise(this.resps.listSharingsMockResp);
|
||||||
};
|
};
|
||||||
|
|
||||||
listSharingIDs = (): Promise<Response<ListSharingIDsResp>> => {
|
listSharingIDs = (): Promise<Response<ListSharingIDsResp>> => {
|
||||||
return this.wrapPromise(this.resps.listSharingIDsMockResp);
|
return this.wrapPromise(this.resps.listSharingIDsMockResp);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
getSharingDir = (): Promise<Response<GetSharingDirResp>> => {
|
||||||
|
return this.wrapPromise(this.resps.getSharingDirMockResp);
|
||||||
|
}
|
||||||
|
|
||||||
isSharing = (dirPath: string): Promise<Response> => {
|
isSharing = (dirPath: string): Promise<Response> => {
|
||||||
return this.wrapPromise(this.resps.isSharingMockResp);
|
return this.wrapPromise(this.resps.isSharingMockResp);
|
||||||
};
|
};
|
||||||
|
|
|
@ -84,6 +84,10 @@ export interface ListSharingIDsResp {
|
||||||
IDs: Map<string, string>;
|
IDs: Map<string, string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface GetSharingDirResp {
|
||||||
|
sharingDir: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface ClientConfigMsg {
|
export interface ClientConfigMsg {
|
||||||
clientCfg: ClientConfig;
|
clientCfg: ClientConfig;
|
||||||
}
|
}
|
||||||
|
@ -142,6 +146,7 @@ export interface IFilesClient {
|
||||||
isSharing: (dirPath: string) => Promise<Response>;
|
isSharing: (dirPath: string) => Promise<Response>;
|
||||||
listSharings: () => Promise<Response<ListSharingsResp>>;
|
listSharings: () => Promise<Response<ListSharingsResp>>;
|
||||||
listSharingIDs: () => Promise<Response<ListSharingIDsResp>>;
|
listSharingIDs: () => Promise<Response<ListSharingIDsResp>>;
|
||||||
|
getSharingDir: (shareID: string) => Promise<Response<GetSharingDirResp>>;
|
||||||
generateHash: (filePath: string) => Promise<Response>;
|
generateHash: (filePath: string) => Promise<Response>;
|
||||||
download: (url: string) => Promise<Response>;
|
download: (url: string) => Promise<Response>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,13 +127,7 @@ export class Updater {
|
||||||
if (resp.status !== 200) {
|
if (resp.status !== 200) {
|
||||||
return errServer;
|
return errServer;
|
||||||
}
|
}
|
||||||
|
this.props.sharingsInfo.sharings = Map<string, string>(resp.data.IDs);
|
||||||
// transform from built-in map to immutable map
|
|
||||||
let sharings = Map<string, string>();
|
|
||||||
resp.data.IDs.forEach((shareID: string, dirPath: string) => {
|
|
||||||
sharings = sharings.set(dirPath, shareID);
|
|
||||||
});
|
|
||||||
this.props.sharingsInfo.sharings = sharings;
|
|
||||||
return "";
|
return "";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -464,8 +458,28 @@ export class Updater {
|
||||||
return "";
|
return "";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
getParamMap = async (params: URLSearchParams): Promise<Map<string, string>> => {
|
||||||
|
let paramMap = Map<string, string>();
|
||||||
|
paramMap = paramMap.set("sh", "");
|
||||||
|
paramMap = paramMap.set("dir", "");
|
||||||
|
|
||||||
|
let shareID = params.get("sh");
|
||||||
|
if (shareID != null && shareID !== "") {
|
||||||
|
paramMap = paramMap.set("sh", shareID);
|
||||||
|
const resp = await this.filesClient.getSharingDir(shareID)
|
||||||
|
if (resp.status === 200) {
|
||||||
|
paramMap = paramMap.set("dir", resp.data.sharingDir);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
paramMap = paramMap.set("dir", params.get("dir"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return paramMap;
|
||||||
|
};
|
||||||
|
|
||||||
initCwd = async (params: URLSearchParams): Promise<string> => {
|
initCwd = async (params: URLSearchParams): Promise<string> => {
|
||||||
const dir = params.get("dir");
|
const paramMap = await this.getParamMap(params)
|
||||||
|
const dir = paramMap.get("dir", "");
|
||||||
|
|
||||||
if (dir != null && dir !== "") {
|
if (dir != null && dir !== "") {
|
||||||
// in sharing mode
|
// in sharing mode
|
||||||
|
|
|
@ -8,16 +8,17 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/ihexxa/quickshare/src/kvstore"
|
"github.com/ihexxa/quickshare/src/kvstore"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
InitNs = "Init"
|
InitNs = "Init"
|
||||||
InfoNs = "sharing"
|
InfoNs = "sharing"
|
||||||
ShareIDNs = "sharingKey"
|
ShareIDNs = "sharingKey"
|
||||||
InitTimeKey = "initTime"
|
InitTimeKey = "initTime"
|
||||||
|
SchemaVerKey = "SchemaVersion"
|
||||||
|
SchemaV1 = "v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -25,6 +26,7 @@ var (
|
||||||
ErrNotFound = errors.New("file info not found")
|
ErrNotFound = errors.New("file info not found")
|
||||||
ErrSharingNotFound = errors.New("sharing id not found")
|
ErrSharingNotFound = errors.New("sharing id not found")
|
||||||
ErrConflicted = errors.New("conflict found in hashing")
|
ErrConflicted = errors.New("conflict found in hashing")
|
||||||
|
ErrVerNotFound = errors.New("file info schema version not found")
|
||||||
maxHashingTime = 10
|
maxHashingTime = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -57,30 +59,99 @@ type FileInfoStore struct {
|
||||||
store kvstore.IKVStore
|
store kvstore.IKVStore
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func migrate(fi *FileInfoStore) error {
|
||||||
|
ver := "v0"
|
||||||
|
schemaVer, ok := fi.store.GetStringIn(InitNs, SchemaVerKey)
|
||||||
|
if ok {
|
||||||
|
ver = schemaVer
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ver {
|
||||||
|
case "v0":
|
||||||
|
// add ShareID to FileInfos
|
||||||
|
infoStrs, err := fi.store.ListStringsIn(InfoNs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
type FileInfoV0 struct {
|
||||||
|
IsDir bool `json:"isDir"`
|
||||||
|
Shared bool `json:"shared"`
|
||||||
|
Sha1 string `json:"sha1"`
|
||||||
|
}
|
||||||
|
|
||||||
|
infoV0 := &FileInfoV0{}
|
||||||
|
for itemPath, infoStr := range infoStrs {
|
||||||
|
err = json.Unmarshal([]byte(infoStr), infoV0)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("list sharing error: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
shareID := ""
|
||||||
|
if infoV0.IsDir && infoV0.Shared {
|
||||||
|
dirShareID, err := fi.getShareID(itemPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
shareID = dirShareID
|
||||||
|
|
||||||
|
err = fi.store.SetStringIn(ShareIDNs, shareID, itemPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
newInfo := &FileInfo{
|
||||||
|
IsDir: infoV0.IsDir,
|
||||||
|
Shared: infoV0.Shared,
|
||||||
|
ShareID: shareID,
|
||||||
|
Sha1: infoV0.Sha1,
|
||||||
|
}
|
||||||
|
if err = fi.SetInfo(itemPath, newInfo); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fi.store.SetStringIn(InitNs, SchemaVerKey, SchemaV1)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case "v1":
|
||||||
|
// no op
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("file info: unknown schema version (%s)", ver)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func NewFileInfoStore(store kvstore.IKVStore) (*FileInfoStore, error) {
|
func NewFileInfoStore(store kvstore.IKVStore) (*FileInfoStore, error) {
|
||||||
_, ok := store.GetStringIn(InitNs, InitTimeKey)
|
var err error
|
||||||
if !ok {
|
for _, nsName := range []string{
|
||||||
var err error
|
InitNs,
|
||||||
for _, nsName := range []string{
|
InfoNs,
|
||||||
InitNs,
|
ShareIDNs,
|
||||||
InfoNs,
|
} {
|
||||||
ShareIDNs,
|
if !store.HasNamespace(nsName) {
|
||||||
} {
|
|
||||||
if err = store.AddNamespace(nsName); err != nil {
|
if err = store.AddNamespace(nsName); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := store.SetStringIn(InitNs, InitTimeKey, fmt.Sprintf("%d", time.Now().Unix()))
|
// err = store.SetStringIn(InitNs, SchemaVerKey, SchemaV1)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return nil, err
|
// return nil, err
|
||||||
}
|
// }
|
||||||
|
|
||||||
return &FileInfoStore{
|
fi := &FileInfoStore{
|
||||||
store: store,
|
store: store,
|
||||||
mtx: &sync.RWMutex{},
|
mtx: &sync.RWMutex{},
|
||||||
}, nil
|
}
|
||||||
|
if err = migrate(fi); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return fi, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fi *FileInfoStore) AddSharing(dirPath string) error {
|
func (fi *FileInfoStore) AddSharing(dirPath string) error {
|
||||||
|
@ -159,6 +230,8 @@ func (fi *FileInfoStore) ListSharings(prefix string) (map[string]string, error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("list sharing error: %w", err)
|
return nil, fmt.Errorf("list sharing error: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println(infoStr)
|
||||||
if info.IsDir && info.Shared {
|
if info.IsDir && info.Shared {
|
||||||
sharings[itemPath] = info.ShareID
|
sharings[itemPath] = info.ShareID
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue