Skip to content

Commit b03f760

Browse files
authored
fix migrator and prefix selector and s3 link (#180)
1 parent 307aaf3 commit b03f760

File tree

11 files changed

+153
-59
lines changed

11 files changed

+153
-59
lines changed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ kodo-browser/
9999

100100
## 4. 私有云配置
101101

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

104104
```json
105105
{

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
"form-data": "^4.0.0",
112112
"js-base64": "^3.4.5",
113113
"js-md5": "^0.7.3",
114-
"kodo-s3-adapter-sdk": "0.5.0",
114+
"kodo-s3-adapter-sdk": "0.5.1",
115115
"lockfile": "^1.0.4",
116116
"lodash": "^4.17.21",
117117
"mime": "^2.3.1",

src/common/const/app-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ export const app = {
99
version: pkgJson.version,
1010
};
1111

12-
export const config_path = path.join(os.homedir(), '.kodo-browser');
12+
export const config_path = path.join(os.homedir(), '.kodo-browser-v2');

src/renderer/modules/update-app/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ async function prevVersionGetter(): Promise<string> {
2020
try {
2121
version = (await fsPromises.readFile(currVersionFilePath)).toString();
2222
} catch {
23-
// v2.1.1 is the first version added migrator
24-
version = "2.1.1";
23+
// v2.1.2 is the first version added migrator
24+
version = "2.1.2";
2525
}
2626
return version;
2727
}

src/renderer/modules/update-app/migrate-steps/2.2.0/downgrade.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import {
2020
appPreferenceKeys,
2121
} from "./types";
2222

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

2526
const new2old: {
2627
[K in keyof AppPreferencesData]: (v: AppPreferencesData[K]) => [SettingStorageKey, string]
@@ -118,7 +119,7 @@ const new2old: {
118119
}
119120

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

136+
async function migrateEndpointConfig() {
137+
const oldFilePath = path.join(oldConfigPath, "config.json");
138+
const newFilePath = path.join(newConfigPath, "config.json");
139+
try {
140+
await fsPromises.copyFile(newFilePath, oldFilePath);
141+
} catch (err: any) {
142+
if (err.code !== "ENOENT") {
143+
throw err;
144+
}
145+
}
146+
}
147+
135148
async function migrateAkHistory() {
136-
const filePath = path.join(configPath, "ak_histories.json");
137-
const contentBuf = await fsPromises.readFile(filePath);
149+
const newFilePath = path.join(newConfigPath, "ak_histories.json");
150+
const oldFilePath = path.join(oldConfigPath, "ak_histories.json");
151+
const contentBuf = await fsPromises.readFile(newFilePath);
138152
const content: AkHistory = JSON.parse(contentBuf.toString());
139153
const oldContent: OldAkHistory = {
140154
historyItems: [],
@@ -147,7 +161,7 @@ async function migrateAkHistory() {
147161
description: i.description,
148162
})
149163
}
150-
await fsPromises.writeFile(filePath, JSON.stringify(content));
164+
await fsPromises.writeFile(oldFilePath, JSON.stringify(content));
151165
}
152166

153167
async function migrateBookmarks(ak: string, filePath: string) {
@@ -164,7 +178,7 @@ async function migrateBookmarks(ak: string, filePath: string) {
164178
});
165179
}
166180
await fsPromises.writeFile(
167-
path.join(configPath, `bookmarks_${ak}.json`),
181+
path.join(oldConfigPath, `bookmarks_${ak}.json`),
168182
JSON.stringify(oldContent),
169183
);
170184
await fsPromises.unlink(filePath);
@@ -190,7 +204,7 @@ async function migrateExternalPaths(ak: string, filePath: string) {
190204
});
191205
}
192206
await fsPromises.writeFile(
193-
path.join(configPath, `external_paths_${ak}.json`),
207+
path.join(oldConfigPath, `external_paths_${ak}.json`),
194208
JSON.stringify(oldContent),
195209
);
196210
await fsPromises.unlink(filePath);
@@ -237,17 +251,29 @@ async function migrateTransferJobs(
237251
}
238252
}
239253
await fsPromises.writeFile(
240-
path.join(configPath, `${type}_${ak}.json`),
254+
path.join(oldConfigPath, `${type}_${ak}.json`),
241255
JSON.stringify(oldContent),
242256
);
243257
await dataStore.close();
244258
}
245259

246260
export default async function () {
247261
await migratePreferences();
262+
263+
try {
264+
await fsPromises.access(oldConfigPath);
265+
} catch (err: any) {
266+
if (err.code !== "ENOENT") {
267+
throw err;
268+
}
269+
await fsPromises.mkdir(oldConfigPath, {recursive: true});
270+
}
271+
272+
273+
await migrateEndpointConfig();
248274
await migrateAkHistory();
249275

250-
const filenames = await fsPromises.readdir(configPath)
276+
const filenames = await fsPromises.readdir(newConfigPath)
251277
const ex = /profile_(?<ak>.+)/;
252278
for (const fName of filenames) {
253279
const matchRes = fName.match(ex);
@@ -256,20 +282,20 @@ export default async function () {
256282
}
257283
await migrateBookmarks(
258284
matchRes.groups["ak"],
259-
path.join(configPath, fName, "bookmarks.json")
285+
path.join(newConfigPath, fName, "bookmarks.json")
260286
);
261287
await migrateExternalPaths(
262288
matchRes.groups["ak"],
263-
path.join(configPath, fName, "external_paths.json")
289+
path.join(newConfigPath, fName, "external_paths.json")
264290
);
265291
await migrateTransferJobs(
266292
matchRes.groups["ak"],
267-
path.join(configPath, fName, "upload_progress"),
293+
path.join(newConfigPath, fName, "upload_progress"),
268294
"upprog",
269295
);
270296
await migrateTransferJobs(
271297
matchRes.groups["ak"],
272-
path.join(configPath, fName, "download_progress"),
298+
path.join(newConfigPath, fName, "download_progress"),
273299
"downprog",
274300
);
275301
}

src/renderer/modules/update-app/migrate-steps/2.2.0/upgrade.ts

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,49 @@ import {
1919
UploadJobPersistInfo,
2020
} from "./types";
2121

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

2425
export default async function () {
26+
try {
27+
await fsPromises.access(newConfigPath);
28+
} catch (err: any) {
29+
if (err.code !== "ENOENT") {
30+
throw err;
31+
}
32+
await fsPromises.mkdir(newConfigPath, {recursive: true});
33+
}
34+
2535
await migratePreferences();
2636

2737
// below migrate date in config path.
2838
// if config path isn't existing, no data need to migrate
2939
try {
30-
await fsPromises.access(configPath);
40+
await fsPromises.access(oldConfigPath);
3141
} catch (err: any) {
3242
if (err.code === "ENOENT") {
3343
return;
3444
}
3545
throw err;
3646
}
3747

48+
await migrateEndpointConfig();
3849
await migrateAkHistory();
3950

40-
const filenames = await fsPromises.readdir(configPath)
51+
const filenames = await fsPromises.readdir(oldConfigPath)
4152
const ex = /(?<type>bookmarks|external_paths|upprog|downprog)_(?<ak>.+).json/;
4253
for (const fName of filenames) {
4354
const matchRes = fName.match(ex);
4455
if (!matchRes || !matchRes.groups?.["ak"]) {
4556
continue;
4657
}
4758
const ak = matchRes.groups["ak"];
48-
const filePath = path.join(configPath, fName);
59+
const filePath = path.join(oldConfigPath, fName);
4960
if (ak === "undefined") {
5061
await fsPromises.unlink(filePath);
5162
continue;
5263
}
53-
const profileDirPath = path.join(configPath, `profile_${ak}`);
64+
const profileDirPath = path.join(newConfigPath, `profile_${ak}`);
5465
try {
5566
await fsPromises.access(profileDirPath);
5667
} catch {
@@ -166,35 +177,51 @@ async function migratePreferences() {
166177
return;
167178
}
168179
try {
169-
await fsPromises.access(configPath);
180+
await fsPromises.access(newConfigPath);
170181
} catch {
171-
await fsPromises.mkdir(configPath);
182+
await fsPromises.mkdir(newConfigPath);
172183
}
173-
const confFilePath = path.join(configPath, "app_preferences.json");
184+
const confFilePath = path.join(newConfigPath, "app_preferences.json");
174185
await fsPromises.writeFile(confFilePath, JSON.stringify(preferences));
175186
}
176187

188+
async function migrateEndpointConfig() {
189+
const oldFilePath = path.join(oldConfigPath, "config.json");
190+
const newFilePath = path.join(newConfigPath, "config.json");
191+
try {
192+
await fsPromises.copyFile(oldFilePath, newFilePath);
193+
} catch (err: any) {
194+
if (err.code !== "ENOENT") {
195+
throw err;
196+
}
197+
}
198+
}
199+
177200
async function migrateAkHistory() {
178-
const filePath = path.join(configPath, "ak_histories.json");
179-
try {
180-
await fsPromises.access(filePath);
201+
const oldFilePath = path.join(oldConfigPath, "ak_histories.json");
202+
const newFilePath = path.join(newConfigPath, "ak_histories.json");
203+
try {
204+
await fsPromises.access(oldFilePath);
181205
} catch {
182206
return;
183207
}
184-
const oldContentBuf = await fsPromises.readFile(filePath);
208+
const oldContentBuf = await fsPromises.readFile(oldFilePath);
185209
const oldContent: OldAkHistory = JSON.parse(oldContentBuf.toString());
186210
const content: AkHistory = {
187211
historyItems: [],
188212
};
189213
for (const i of oldContent.historyItems) {
214+
if (!i.accessKeyId || !i.accessKeySecret) {
215+
continue;
216+
}
190217
content.historyItems.push({
191218
endpointType: i.isPublicCloud ? "public" : "private",
192219
accessKey: i.accessKeyId,
193220
accessSecret: i.accessKeySecret,
194221
description: i.description,
195-
})
222+
});
196223
}
197-
await fsPromises.writeFile(filePath, JSON.stringify(content));
224+
await fsPromises.writeFile(newFilePath, JSON.stringify(content));
198225
}
199226

200227
const pathEx = /(?<protocol>[a-zA-Z0-9\-_]+:\/\/)(?<path>.+)/;
@@ -206,19 +233,21 @@ async function migrateBookmarks(ak: string, filePath: string) {
206233
homeAddress: oldContent.homeAddress,
207234
list: [],
208235
};
209-
for (const b of oldContent.bookmarks) {
210-
const exRes = b.fullPath.match(pathEx);
211-
if (!exRes || !exRes.groups) {
212-
continue;
236+
if (Array.isArray(oldContent.bookmarks)) {
237+
for (const b of oldContent.bookmarks) {
238+
const exRes = b.fullPath.match(pathEx);
239+
if (!exRes || !exRes.groups) {
240+
continue;
241+
}
242+
content.list.push({
243+
protocol: exRes.groups["protocol"],
244+
path: exRes.groups["path"],
245+
timestamp: b.timestamp,
246+
});
213247
}
214-
content.list.push({
215-
protocol: exRes.groups["protocol"],
216-
path: exRes.groups["path"],
217-
timestamp: b.timestamp,
218-
});
219248
}
220249
await fsPromises.writeFile(
221-
path.join(configPath, `profile_${ak}`, "bookmarks.json"),
250+
path.join(newConfigPath, `profile_${ak}`, "bookmarks.json"),
222251
JSON.stringify(content),
223252
);
224253
await fsPromises.unlink(filePath);
@@ -242,7 +271,7 @@ async function migrateExternalPaths(ak: string, filePath: string) {
242271
});
243272
}
244273
await fsPromises.writeFile(
245-
path.join(configPath, `profile_${ak}`, "external_paths.json"),
274+
path.join(newConfigPath, `profile_${ak}`, "external_paths.json"),
246275
JSON.stringify(content),
247276
);
248277
await fsPromises.unlink(filePath);
@@ -270,7 +299,7 @@ function migrateJobInfo(
270299
if (isUploadJob(oldInfo)) {
271300
info = {
272301
...oldInfo,
273-
uploadedParts: oldInfo.uploadedParts.map(p => ({
302+
uploadedParts: (oldInfo.uploadedParts || []).map(p => ({
274303
partNumber: p.PartNumber,
275304
etag: p.ETag,
276305
})),
@@ -286,7 +315,7 @@ function migrateJobInfo(
286315
info.status = ["waiting", "running"].includes(oldInfo.status)
287316
? "stopped"
288317
: oldInfo.status;
289-
info.storageClasses = oldInfo.storageClasses.map(c => ({
318+
info.storageClasses = (oldInfo.storageClasses || []).map(c => ({
290319
kodoName: c.kodoName,
291320
fileType: c.fileType,
292321
s3Name: c.s3Name,
@@ -299,15 +328,19 @@ async function migrateTransferJobs(
299328
filePath: string,
300329
type: "upload_prog" | "download_prog",
301330
) {
302-
const basedir = path.join(configPath, `profile_${ak}`, type);
331+
const basedir = path.join(newConfigPath, `profile_${ak}`, type);
303332
const contentBuf = await fsPromises.readFile(filePath);
304333
const oldContent = JSON.parse(contentBuf.toString()) as Record<string, OldUploadJobPersistInfo | OldDownloadJobPersistInfo>;
305334
const dataStore = await getDataStoreOrCreate<UploadJobPersistInfo | DownloadJobPersistInfo>({
306335
workingDirectory: basedir,
307336
});
308337
for (const [oldId, oldInfo] of Object.entries(oldContent)) {
309-
const [id, info] = migrateJobInfo(oldId, oldInfo);
310-
await dataStore.set(id, info);
338+
try {
339+
const [id, info] = migrateJobInfo(oldId, oldInfo);
340+
await dataStore.set(id, info);
341+
} catch {
342+
// skip error data
343+
}
311344
}
312345
await dataStore.compact(true);
313346
await dataStore.close();

src/renderer/pages/browse/files/file-grid/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ const FileGrid: React.FC<FileGridProps> = ({
5353
.map(p => p.path.toString());
5454
const filesData = data.map((item, index) => {
5555
const itemPath = item.path.toString();
56+
const selectedItemId = [item.itemType, itemPath].join(":");
5657
const prefixHit = prefixPaths.some(p => itemPath.startsWith(p));
57-
const selectHit = selectedFiles.has(itemPath) && !FileItem.isItemPrefix(selectedFiles.get(itemPath));
58+
const selectHit = selectedFiles.has(selectedItemId);
5859
return {
5960
...item,
6061
id: itemPath,

0 commit comments

Comments
 (0)