Skip to content

Commit

Permalink
fix migrator and prefix selector and s3 link (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihsai0 authored May 8, 2024
1 parent 307aaf3 commit b03f760
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ kodo-browser/

## 4. 私有云配置

将配置文件放在 `$HOME/.kodo-browser/config.json`(如果是 Windows 10,则位置是 `C:\Users\<UserName>\.kodo-browser\config.json`)下,配置文件示例如下:
将配置文件放在 `$HOME/.kodo-browser-v2/config.json`(如果是 Windows 10,则位置是 `C:\Users\<UserName>\.kodo-browser-v2\config.json`)下,配置文件示例如下:

```json
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"form-data": "^4.0.0",
"js-base64": "^3.4.5",
"js-md5": "^0.7.3",
"kodo-s3-adapter-sdk": "0.5.0",
"kodo-s3-adapter-sdk": "0.5.1",
"lockfile": "^1.0.4",
"lodash": "^4.17.21",
"mime": "^2.3.1",
Expand Down
2 changes: 1 addition & 1 deletion src/common/const/app-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export const app = {
version: pkgJson.version,
};

export const config_path = path.join(os.homedir(), '.kodo-browser');
export const config_path = path.join(os.homedir(), '.kodo-browser-v2');
4 changes: 2 additions & 2 deletions src/renderer/modules/update-app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ async function prevVersionGetter(): Promise<string> {
try {
version = (await fsPromises.readFile(currVersionFilePath)).toString();
} catch {
// v2.1.1 is the first version added migrator
version = "2.1.1";
// v2.1.2 is the first version added migrator
version = "2.1.2";
}
return version;
}
Expand Down
52 changes: 39 additions & 13 deletions src/renderer/modules/update-app/migrate-steps/2.2.0/downgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
appPreferenceKeys,
} from "./types";

const configPath = path.join(os.homedir(), ".kodo-browser");
const oldConfigPath = path.join(os.homedir(), ".kodo-browser");
const newConfigPath = path.join(os.homedir(), ".kodo-browser-v2");

const new2old: {
[K in keyof AppPreferencesData]: (v: AppPreferencesData[K]) => [SettingStorageKey, string]
Expand Down Expand Up @@ -118,7 +119,7 @@ const new2old: {
}

async function migratePreferences() {
const confFilePath = path.join(configPath, "app_preferences.json");
const confFilePath = path.join(newConfigPath, "app_preferences.json");
const confBuf = await fsPromises.readFile(confFilePath);
const conf: Partial<AppPreferencesData> = JSON.parse(confBuf.toString());
for (const k of appPreferenceKeys) {
Expand All @@ -132,9 +133,22 @@ async function migratePreferences() {
await fsPromises.unlink(confFilePath);
}

async function migrateEndpointConfig() {
const oldFilePath = path.join(oldConfigPath, "config.json");
const newFilePath = path.join(newConfigPath, "config.json");
try {
await fsPromises.copyFile(newFilePath, oldFilePath);
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
}
}

async function migrateAkHistory() {
const filePath = path.join(configPath, "ak_histories.json");
const contentBuf = await fsPromises.readFile(filePath);
const newFilePath = path.join(newConfigPath, "ak_histories.json");
const oldFilePath = path.join(oldConfigPath, "ak_histories.json");
const contentBuf = await fsPromises.readFile(newFilePath);
const content: AkHistory = JSON.parse(contentBuf.toString());
const oldContent: OldAkHistory = {
historyItems: [],
Expand All @@ -147,7 +161,7 @@ async function migrateAkHistory() {
description: i.description,
})
}
await fsPromises.writeFile(filePath, JSON.stringify(content));
await fsPromises.writeFile(oldFilePath, JSON.stringify(content));
}

async function migrateBookmarks(ak: string, filePath: string) {
Expand All @@ -164,7 +178,7 @@ async function migrateBookmarks(ak: string, filePath: string) {
});
}
await fsPromises.writeFile(
path.join(configPath, `bookmarks_${ak}.json`),
path.join(oldConfigPath, `bookmarks_${ak}.json`),
JSON.stringify(oldContent),
);
await fsPromises.unlink(filePath);
Expand All @@ -190,7 +204,7 @@ async function migrateExternalPaths(ak: string, filePath: string) {
});
}
await fsPromises.writeFile(
path.join(configPath, `external_paths_${ak}.json`),
path.join(oldConfigPath, `external_paths_${ak}.json`),
JSON.stringify(oldContent),
);
await fsPromises.unlink(filePath);
Expand Down Expand Up @@ -237,17 +251,29 @@ async function migrateTransferJobs(
}
}
await fsPromises.writeFile(
path.join(configPath, `${type}_${ak}.json`),
path.join(oldConfigPath, `${type}_${ak}.json`),
JSON.stringify(oldContent),
);
await dataStore.close();
}

export default async function () {
await migratePreferences();

try {
await fsPromises.access(oldConfigPath);
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
await fsPromises.mkdir(oldConfigPath, {recursive: true});
}


await migrateEndpointConfig();
await migrateAkHistory();

const filenames = await fsPromises.readdir(configPath)
const filenames = await fsPromises.readdir(newConfigPath)
const ex = /profile_(?<ak>.+)/;
for (const fName of filenames) {
const matchRes = fName.match(ex);
Expand All @@ -256,20 +282,20 @@ export default async function () {
}
await migrateBookmarks(
matchRes.groups["ak"],
path.join(configPath, fName, "bookmarks.json")
path.join(newConfigPath, fName, "bookmarks.json")
);
await migrateExternalPaths(
matchRes.groups["ak"],
path.join(configPath, fName, "external_paths.json")
path.join(newConfigPath, fName, "external_paths.json")
);
await migrateTransferJobs(
matchRes.groups["ak"],
path.join(configPath, fName, "upload_progress"),
path.join(newConfigPath, fName, "upload_progress"),
"upprog",
);
await migrateTransferJobs(
matchRes.groups["ak"],
path.join(configPath, fName, "download_progress"),
path.join(newConfigPath, fName, "download_progress"),
"downprog",
);
}
Expand Down
93 changes: 63 additions & 30 deletions src/renderer/modules/update-app/migrate-steps/2.2.0/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,49 @@ import {
UploadJobPersistInfo,
} from "./types";

const configPath = path.join(os.homedir(), ".kodo-browser");
const oldConfigPath = path.join(os.homedir(), ".kodo-browser");
const newConfigPath = path.join(os.homedir(), ".kodo-browser-v2");

export default async function () {
try {
await fsPromises.access(newConfigPath);
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
await fsPromises.mkdir(newConfigPath, {recursive: true});
}

await migratePreferences();

// below migrate date in config path.
// if config path isn't existing, no data need to migrate
try {
await fsPromises.access(configPath);
await fsPromises.access(oldConfigPath);
} catch (err: any) {
if (err.code === "ENOENT") {
return;
}
throw err;
}

await migrateEndpointConfig();
await migrateAkHistory();

const filenames = await fsPromises.readdir(configPath)
const filenames = await fsPromises.readdir(oldConfigPath)
const ex = /(?<type>bookmarks|external_paths|upprog|downprog)_(?<ak>.+).json/;
for (const fName of filenames) {
const matchRes = fName.match(ex);
if (!matchRes || !matchRes.groups?.["ak"]) {
continue;
}
const ak = matchRes.groups["ak"];
const filePath = path.join(configPath, fName);
const filePath = path.join(oldConfigPath, fName);
if (ak === "undefined") {
await fsPromises.unlink(filePath);
continue;
}
const profileDirPath = path.join(configPath, `profile_${ak}`);
const profileDirPath = path.join(newConfigPath, `profile_${ak}`);
try {
await fsPromises.access(profileDirPath);
} catch {
Expand Down Expand Up @@ -166,35 +177,51 @@ async function migratePreferences() {
return;
}
try {
await fsPromises.access(configPath);
await fsPromises.access(newConfigPath);
} catch {
await fsPromises.mkdir(configPath);
await fsPromises.mkdir(newConfigPath);
}
const confFilePath = path.join(configPath, "app_preferences.json");
const confFilePath = path.join(newConfigPath, "app_preferences.json");
await fsPromises.writeFile(confFilePath, JSON.stringify(preferences));
}

async function migrateEndpointConfig() {
const oldFilePath = path.join(oldConfigPath, "config.json");
const newFilePath = path.join(newConfigPath, "config.json");
try {
await fsPromises.copyFile(oldFilePath, newFilePath);
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
}
}

async function migrateAkHistory() {
const filePath = path.join(configPath, "ak_histories.json");
try {
await fsPromises.access(filePath);
const oldFilePath = path.join(oldConfigPath, "ak_histories.json");
const newFilePath = path.join(newConfigPath, "ak_histories.json");
try {
await fsPromises.access(oldFilePath);
} catch {
return;
}
const oldContentBuf = await fsPromises.readFile(filePath);
const oldContentBuf = await fsPromises.readFile(oldFilePath);
const oldContent: OldAkHistory = JSON.parse(oldContentBuf.toString());
const content: AkHistory = {
historyItems: [],
};
for (const i of oldContent.historyItems) {
if (!i.accessKeyId || !i.accessKeySecret) {
continue;
}
content.historyItems.push({
endpointType: i.isPublicCloud ? "public" : "private",
accessKey: i.accessKeyId,
accessSecret: i.accessKeySecret,
description: i.description,
})
});
}
await fsPromises.writeFile(filePath, JSON.stringify(content));
await fsPromises.writeFile(newFilePath, JSON.stringify(content));
}

const pathEx = /(?<protocol>[a-zA-Z0-9\-_]+:\/\/)(?<path>.+)/;
Expand All @@ -206,19 +233,21 @@ async function migrateBookmarks(ak: string, filePath: string) {
homeAddress: oldContent.homeAddress,
list: [],
};
for (const b of oldContent.bookmarks) {
const exRes = b.fullPath.match(pathEx);
if (!exRes || !exRes.groups) {
continue;
if (Array.isArray(oldContent.bookmarks)) {
for (const b of oldContent.bookmarks) {
const exRes = b.fullPath.match(pathEx);
if (!exRes || !exRes.groups) {
continue;
}
content.list.push({
protocol: exRes.groups["protocol"],
path: exRes.groups["path"],
timestamp: b.timestamp,
});
}
content.list.push({
protocol: exRes.groups["protocol"],
path: exRes.groups["path"],
timestamp: b.timestamp,
});
}
await fsPromises.writeFile(
path.join(configPath, `profile_${ak}`, "bookmarks.json"),
path.join(newConfigPath, `profile_${ak}`, "bookmarks.json"),
JSON.stringify(content),
);
await fsPromises.unlink(filePath);
Expand All @@ -242,7 +271,7 @@ async function migrateExternalPaths(ak: string, filePath: string) {
});
}
await fsPromises.writeFile(
path.join(configPath, `profile_${ak}`, "external_paths.json"),
path.join(newConfigPath, `profile_${ak}`, "external_paths.json"),
JSON.stringify(content),
);
await fsPromises.unlink(filePath);
Expand Down Expand Up @@ -270,7 +299,7 @@ function migrateJobInfo(
if (isUploadJob(oldInfo)) {
info = {
...oldInfo,
uploadedParts: oldInfo.uploadedParts.map(p => ({
uploadedParts: (oldInfo.uploadedParts || []).map(p => ({
partNumber: p.PartNumber,
etag: p.ETag,
})),
Expand All @@ -286,7 +315,7 @@ function migrateJobInfo(
info.status = ["waiting", "running"].includes(oldInfo.status)
? "stopped"
: oldInfo.status;
info.storageClasses = oldInfo.storageClasses.map(c => ({
info.storageClasses = (oldInfo.storageClasses || []).map(c => ({
kodoName: c.kodoName,
fileType: c.fileType,
s3Name: c.s3Name,
Expand All @@ -299,15 +328,19 @@ async function migrateTransferJobs(
filePath: string,
type: "upload_prog" | "download_prog",
) {
const basedir = path.join(configPath, `profile_${ak}`, type);
const basedir = path.join(newConfigPath, `profile_${ak}`, type);
const contentBuf = await fsPromises.readFile(filePath);
const oldContent = JSON.parse(contentBuf.toString()) as Record<string, OldUploadJobPersistInfo | OldDownloadJobPersistInfo>;
const dataStore = await getDataStoreOrCreate<UploadJobPersistInfo | DownloadJobPersistInfo>({
workingDirectory: basedir,
});
for (const [oldId, oldInfo] of Object.entries(oldContent)) {
const [id, info] = migrateJobInfo(oldId, oldInfo);
await dataStore.set(id, info);
try {
const [id, info] = migrateJobInfo(oldId, oldInfo);
await dataStore.set(id, info);
} catch {
// skip error data
}
}
await dataStore.compact(true);
await dataStore.close();
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/pages/browse/files/file-grid/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ const FileGrid: React.FC<FileGridProps> = ({
.map(p => p.path.toString());
const filesData = data.map((item, index) => {
const itemPath = item.path.toString();
const selectedItemId = [item.itemType, itemPath].join(":");
const prefixHit = prefixPaths.some(p => itemPath.startsWith(p));
const selectHit = selectedFiles.has(itemPath) && !FileItem.isItemPrefix(selectedFiles.get(itemPath));
const selectHit = selectedFiles.has(selectedItemId);
return {
...item,
id: itemPath,
Expand Down
Loading

0 comments on commit b03f760

Please sign in to comment.