diff --git a/package.json b/package.json index d4581123b0..56be52b671 100644 --- a/package.json +++ b/package.json @@ -175,6 +175,7 @@ "crypto-js": "^4.0.0", "electron-updater": "^4.3.1", "electron-window-state": "^5.0.3", + "file-type": "^14.7.1", "filenamify": "^4.1.0", "fs-extra": "^9.0.1", "handlebars": "^4.7.6", diff --git a/src/migrations/cluster-store/2.0.0-beta.2.ts b/src/migrations/cluster-store/2.0.0-beta.2.ts index 8a01af5407..6f78505a6c 100644 --- a/src/migrations/cluster-store/2.0.0-beta.2.ts +++ b/src/migrations/cluster-store/2.0.0-beta.2.ts @@ -5,7 +5,7 @@ import { migration } from "../migration-wrapper"; export default migration({ version: "2.0.0-beta.2", - run(store, log) { + async run(store, log) { for (const value of store) { const contextName = value[0]; // Looping all the keys gives out the store internal stuff too... diff --git a/src/migrations/cluster-store/2.4.1.ts b/src/migrations/cluster-store/2.4.1.ts index 5789f6cc36..329efc2980 100644 --- a/src/migrations/cluster-store/2.4.1.ts +++ b/src/migrations/cluster-store/2.4.1.ts @@ -3,7 +3,7 @@ import { migration } from "../migration-wrapper"; export default migration({ version: "2.4.1", - run(store, log) { + async run(store, log) { for (const value of store) { const contextName = value[0]; if (contextName === "__internal__") continue; diff --git a/src/migrations/cluster-store/2.6.0-beta.2.ts b/src/migrations/cluster-store/2.6.0-beta.2.ts index 0e13afe7a9..2dab757b16 100644 --- a/src/migrations/cluster-store/2.6.0-beta.2.ts +++ b/src/migrations/cluster-store/2.6.0-beta.2.ts @@ -3,7 +3,7 @@ import { migration } from "../migration-wrapper"; export default migration({ version: "2.6.0-beta.2", - run(store, log) { + async run(store, log) { for (const value of store) { const clusterKey = value[0]; if (clusterKey === "__internal__") continue diff --git a/src/migrations/cluster-store/2.6.0-beta.3.ts b/src/migrations/cluster-store/2.6.0-beta.3.ts index 11f1a3bce9..cff75137d9 100644 --- a/src/migrations/cluster-store/2.6.0-beta.3.ts +++ b/src/migrations/cluster-store/2.6.0-beta.3.ts @@ -3,7 +3,7 @@ import yaml from "js-yaml" export default migration({ version: "2.6.0-beta.3", - run(store, log) { + async run(store, log) { for (const value of store) { const clusterKey = value[0]; if (clusterKey === "__internal__") continue diff --git a/src/migrations/cluster-store/2.7.0-beta.0.ts b/src/migrations/cluster-store/2.7.0-beta.0.ts index 22c4e6bba9..0d8db256bb 100644 --- a/src/migrations/cluster-store/2.7.0-beta.0.ts +++ b/src/migrations/cluster-store/2.7.0-beta.0.ts @@ -3,10 +3,10 @@ import { migration } from "../migration-wrapper"; export default migration({ version: "2.7.0-beta.0", - run(store, log) { + async run(store, log) { for (const value of store) { const clusterKey = value[0]; - if(clusterKey === "__internal__") continue + if (clusterKey === "__internal__") continue const cluster = value[1]; cluster.workspace = "default" store.set(clusterKey, cluster) diff --git a/src/migrations/cluster-store/2.7.0-beta.1.ts b/src/migrations/cluster-store/2.7.0-beta.1.ts index de9e4506d1..9368201a11 100644 --- a/src/migrations/cluster-store/2.7.0-beta.1.ts +++ b/src/migrations/cluster-store/2.7.0-beta.1.ts @@ -4,7 +4,7 @@ import { v4 as uuid } from "uuid" export default migration({ version: "2.7.0-beta.1", - run(store, log) { + async run(store, log) { const clusters: any[] = [] for (const value of store) { const clusterKey = value[0]; diff --git a/src/migrations/cluster-store/3.6.0-beta.1.ts b/src/migrations/cluster-store/3.6.0-beta.1.ts index 5f9f4cf64d..378a5aaf51 100644 --- a/src/migrations/cluster-store/3.6.0-beta.1.ts +++ b/src/migrations/cluster-store/3.6.0-beta.1.ts @@ -5,21 +5,22 @@ import path from "path" import { app, remote } from "electron" import { migration } from "../migration-wrapper"; import fse from "fs-extra" +import fileType from "file-type"; import { ClusterModel } from "../../common/cluster-store"; import { loadConfig, saveConfigToAppFiles } from "../../common/kube-helpers"; export default migration({ version: "3.6.0-beta.1", - run(store, printLog) { + async run(store, printLog) { const kubeConfigBase = path.join((app || remote.app).getPath("userData"), "kubeconfigs") const storedClusters: ClusterModel[] = store.get("clusters") || []; - if (!storedClusters?.length) return; + if (!storedClusters.length) return; fse.ensureDirSync(kubeConfigBase); printLog("Number of clusters to migrate: ", storedClusters.length) const migratedClusters = storedClusters - .map(cluster => { + .map(async cluster => { /** * migrate kubeconfig */ @@ -30,7 +31,7 @@ export default migration({ delete cluster.kubeConfig; } catch (error) { - printLog(`Failed to migrate Kubeconfig for cluster "${cluster.id}"`, error) + printLog(`Failed to migrate Kubeconfig for cluster "${cluster.id}", removing cluster...`, error) return undefined; } @@ -39,14 +40,20 @@ export default migration({ */ try { if (cluster.preferences.icon) { - const fileData = fse.readFileSync(cluster.preferences.icon); - cluster.preferences.icon = `data:image;base64, ${fileData.toString('base64')}`; + const fileData = await fse.readFile(cluster.preferences.icon); + const { mime } = await fileType.fromBuffer(fileData); + + if (!mime) { + throw "unknown icon file type, deleting..."; + } + + cluster.preferences.icon = `data:${mime};base64, ${fileData.toString('base64')}`; } else { delete cluster.preferences.icon; } } catch (error) { printLog(`Failed to migrate cluster icon for cluster "${cluster.id}"`, error) - return undefined; + delete cluster.preferences.icon; } return cluster; diff --git a/src/migrations/migration-wrapper.ts b/src/migrations/migration-wrapper.ts index 39015b81fb..41c95d5359 100644 --- a/src/migrations/migration-wrapper.ts +++ b/src/migrations/migration-wrapper.ts @@ -3,7 +3,7 @@ import { isTestEnv } from "../common/vars"; export interface MigrationOpts { version: string; - run(storeConfig: Config, log: (...args: any[]) => void): void; + run(storeConfig: Config, log: (...args: any[]) => void): Promise; } function infoLog(...args: any[]) { @@ -13,9 +13,9 @@ function infoLog(...args: any[]) { export function migration({ version, run }: MigrationOpts) { return { - [version]: (storeConfig: Config) => { + [version]: async (storeConfig: Config) => { infoLog(`STORE MIGRATION (${storeConfig.path}): ${version}`,); - run(storeConfig, infoLog); + await run(storeConfig, infoLog); } }; } diff --git a/src/migrations/user-store/2.1.0-beta.4.ts b/src/migrations/user-store/2.1.0-beta.4.ts index 24c4cde5e3..ce11e0d818 100644 --- a/src/migrations/user-store/2.1.0-beta.4.ts +++ b/src/migrations/user-store/2.1.0-beta.4.ts @@ -3,7 +3,7 @@ import { migration } from "../migration-wrapper"; export default migration({ version: "2.1.0-beta.4", - run(store) { + async run(store) { store.set("lastSeenAppVersion", "0.0.0"); } }) diff --git a/src/renderer/components/+cluster-settings/components/cluster-icon-setting.tsx b/src/renderer/components/+cluster-settings/components/cluster-icon-setting.tsx index d638a1ab09..04b7ef1fc6 100644 --- a/src/renderer/components/+cluster-settings/components/cluster-icon-setting.tsx +++ b/src/renderer/components/+cluster-settings/components/cluster-icon-setting.tsx @@ -28,7 +28,7 @@ export class ClusterIconSetting extends React.Component { try { if (file) { const buf = Buffer.from(await file.arrayBuffer()); - cluster.preferences.icon = `data:image;base64, ${buf.toString('base64')}`; + cluster.preferences.icon = `data:${file.type};base64, ${buf.toString('base64')}`; } else { // this has to be done as a seperate branch (and not always) because `cluster` // is observable and triggers an update loop. diff --git a/yarn.lock b/yarn.lock index d8881d0f1d..b4a04a92b5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1588,6 +1588,11 @@ dependencies: defer-to-connect "^1.0.1" +"@tokenizer/token@^0.1.0", "@tokenizer/token@^0.1.1": + version "0.1.1" + resolved "https://registry.yarnpkg.com/@tokenizer/token/-/token-0.1.1.tgz#f0d92c12f87079ddfd1b29f614758b9696bc29e3" + integrity sha512-XO6INPbZCxdprl+9qa/AAbFFOMzzwqYxpjPgLICrMD6C2FCw6qfJOPcBk6JqqPLSaZ/Qx87qn4rpPmPMwaAK6w== + "@types/anymatch@*": version "1.3.1" resolved "https://registry.yarnpkg.com/@types/anymatch/-/anymatch-1.3.1.tgz#336badc1beecb9dacc38bea2cf32adf627a8421a" @@ -5189,6 +5194,16 @@ file-loader@^6.0.0: loader-utils "^2.0.0" schema-utils "^2.6.5" +file-type@^14.7.1: + version "14.7.1" + resolved "https://registry.yarnpkg.com/file-type/-/file-type-14.7.1.tgz#f748732b3e70478bff530e1cf0ec2fe33608b1bb" + integrity sha512-sXAMgFk67fQLcetXustxfKX+PZgHIUFn96Xld9uH8aXPdX3xOp0/jg9OdouVTvQrf7mrn+wAa4jN/y9fUOOiRA== + dependencies: + readable-web-to-node-stream "^2.0.0" + strtok3 "^6.0.3" + token-types "^2.0.0" + typedarray-to-buffer "^3.1.5" + file-uri-to-path@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd" @@ -6067,7 +6082,7 @@ identity-obj-proxy@^3.0.0: dependencies: harmony-reflect "^1.4.6" -ieee754@^1.1.4: +ieee754@^1.1.13, ieee754@^1.1.4: version "1.1.13" resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.1.13.tgz#ec168558e95aa181fd87d37f55c32bbcb6708b84" integrity sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg== @@ -8936,6 +8951,11 @@ pbkdf2@^3.0.3: safe-buffer "^5.0.1" sha.js "^2.4.8" +peek-readable@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/peek-readable/-/peek-readable-3.1.0.tgz#250b08b7de09db8573d7fd8ea475215bbff14348" + integrity sha512-KGuODSTV6hcgdZvDrIDBUkN0utcAVj1LL7FfGbM0viKTtCHmtZcuEJ+lGqsp0fTFkGqesdtemV2yUSMeyy3ddA== + pend@~1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/pend/-/pend-1.2.0.tgz#7a57eb550a6783f9115331fcf4663d5c8e007a50" @@ -10648,6 +10668,15 @@ strip-outer@^1.0.1: dependencies: escape-string-regexp "^1.0.2" +strtok3@^6.0.3: + version "6.0.4" + resolved "https://registry.yarnpkg.com/strtok3/-/strtok3-6.0.4.tgz#ede0d20fde5aa9fda56417c3558eaafccc724694" + integrity sha512-rqWMKwsbN9APU47bQTMEYTPcwdpKDtmf1jVhHzNW2cL1WqAxaM9iBb9t5P2fj+RV2YsErUWgQzHD5JwV0uCTEQ== + dependencies: + "@tokenizer/token" "^0.1.1" + "@types/debug" "^4.1.5" + peek-readable "^3.1.0" + style-loader@^1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/style-loader/-/style-loader-1.2.1.tgz#c5cbbfbf1170d076cfdd86e0109c5bba114baa1a" @@ -10977,6 +11006,14 @@ to-regex@^3.0.1, to-regex@^3.0.2: regex-not "^1.0.2" safe-regex "^1.1.0" +token-types@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/token-types/-/token-types-2.0.0.tgz#b23618af744818299c6fbf125e0fdad98bab7e85" + integrity sha512-WWvu8sGK8/ZmGusekZJJ5NM6rRVTTDO7/bahz4NGiSDb/XsmdYBn6a1N/bymUHuWYTWeuLUg98wUzvE4jPdCZw== + dependencies: + "@tokenizer/token" "^0.1.0" + ieee754 "^1.1.13" + touch@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/touch/-/touch-3.1.0.tgz#fe365f5f75ec9ed4e56825e0bb76d24ab74af83b"