merge main

This commit is contained in:
neil molina 2023-04-20 13:03:26 +08:00
commit 2c9f276b6e
8 changed files with 78 additions and 6 deletions

View file

@ -1,5 +1,5 @@
import { ipcMain, app, BrowserWindow } from "electron";
import { deletePackageFolder, getInstalledPackages } from "./tea-dir";
import { deletePackageFolder, getInstalledPackages, cacheImage } from "./tea-dir";
import { readSessionData, writeSessionData } from "./auth";
import type { Packages, Session } from "../../src/libs/types";
import * as log from "electron-log";
@ -181,4 +181,15 @@ export default function initializeHandlers() {
mainWindow.isMaximized() ? mainWindow.unmaximize() : mainWindow.maximize();
}
});
ipcMain.handle("cache-image", async (_event, url) => {
try {
log.info("caching:", url);
const cachedImagePath = await cacheImage(url);
return cachedImagePath;
} catch (error) {
log.error("Failed to cache image:", error);
throw error;
}
});
}

View file

@ -7,6 +7,7 @@ import * as log from "electron-log";
import type { InstalledPackage } from "../../src/libs/types";
import semverCompare from "semver/functions/compare";
import { mkdirp } from "mkdirp";
import fetch from "node-fetch";
type Dir = {
name: string;
@ -170,3 +171,34 @@ export async function deletePackageFolder(fullName, version) {
log.error(error);
}
}
async function downloadImage(url: string, imagePath: string): Promise<void> {
const response = await fetch(url);
await new Promise<void>((resolve, reject) => {
const fileStream = fs.createWriteStream(imagePath);
response.body.pipe(fileStream);
fileStream.on("finish", () => resolve());
fileStream.on("error", (error) => reject(error));
});
}
export async function cacheImage(url: string): Promise<string> {
const imageFolder = path.join(getGuiPath(), "cached_images");
const imageName = path.basename(url);
const imagePath = path.join(imageFolder, imageName);
await mkdirp(imageFolder);
if (!fs.existsSync(imagePath)) {
try {
await downloadImage(url, imagePath);
console.log("Image downloaded and cached:", imagePath);
} catch (error) {
console.error("Failed to download image:", error);
}
} else {
console.log("Image already cached:", imagePath);
}
return `file://${imagePath}`;
}

View file

@ -5,14 +5,16 @@
import { findRecentInstalledVersion } from "$libs/packages/pkg-utils";
import PackageInstallButton from "$components/package-install-button/package-install-button.svelte";
import PackageInstalledBadge from "$components/package-install-button/package-installed-badge.svelte";
import { onMount } from "svelte";
import { packagesStore } from "$libs/stores";
export let pkg: GUIPackage;
export let link: string;
export let progessLoading = 0;
$: imgUrl = !pkg.thumb_image_url.includes("https://tea.xyz")
$: imgUrl = pkg?.cached_image_url || (!pkg.thumb_image_url.includes("https://tea.xyz")
? "https://tea.xyz/Images/package-thumb-nolabel4.jpg"
: pkg.thumb_image_url;
: pkg.thumb_image_url);
export let onClickCTA = async () => {
console.log("do nothing");
@ -28,6 +30,10 @@
const deactivate = () => (isActive = false);
const preventPropagation = (evt: MouseEvent) => evt.stopPropagation();
onMount(() => {
if (pkg && !pkg?.cached_image_url) packagesStore.cachePkgImage(pkg);
});
</script>
<section

View file

@ -10,7 +10,7 @@
const submitBugReport = async () => {
const logId = await submitLogs();
const bugFormUrl = `https://airtable.com/shravDxWeNwwpPkFV?prefill_log_id=${logId}`;
const bugFormUrl = `https://airtable.com/shravDxWeNwwpPkFV?prefill_log_id=${logId}&hide_log_id=true`;
shellOpenExternal(bugFormUrl);
}
</script>

View file

@ -260,3 +260,12 @@ export const topbarDoubleClick = async () => {
log.error(error);
}
};
export const cacheImageURL = async (url: string): Promise<string | undefined> => {
try {
const cachedSrc = await ipcRenderer.invoke("cache-image", url);
return cachedSrc;
} catch (error) {
log.error("Failed to cache image:", error);
}
};

View file

@ -379,3 +379,7 @@ export const writePackageCache = async (pkgs: Packages) => {
export const topbarDoubleClick = async () => {
console.log("topbar double click");
};
export const cacheImageURL = async (url: string): Promise<string | undefined> => {
return undefined;
};

View file

@ -12,7 +12,8 @@ import {
setBadgeCount,
loadPackageCache,
writePackageCache,
syncPantry
syncPantry,
cacheImageURL
} from "@native";
import { getReadme, getContributors, getRepoAsPackage } from "$libs/github";
@ -250,6 +251,13 @@ To read more about this package go to [${guiPkg.homepage}](${guiPkg.homepage}).
writePackageCacheWithDebounce(pkgs);
});
const cachePkgImage = async (pkg: GUIPackage) => {
const cacheFileURL = await cacheImageURL(pkg.thumb_image_url);
if (cacheFileURL) {
updatePackage(pkg.full_name, { cached_image_url: cacheFileURL });
}
};
return {
packageList,
syncProgress,
@ -267,7 +275,8 @@ To read more about this package go to [${guiPkg.homepage}](${guiPkg.homepage}).
uninstallPkg,
syncPackageData,
deletePkg,
destroy
destroy,
cachePkgImage
};
}

View file

@ -24,6 +24,7 @@ export type GUIPackage = Package & {
installed_versions?: string[];
synced?: boolean;
install_progress_percentage?: number;
cached_image_url?: string;
};
export type Course = {