1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-07-13 00:10:37 +02:00

Desktop: Seamless-Updates: used download function from tool-utils (#11066)

This commit is contained in:
Alice
2024-09-17 00:13:24 +03:00
committed by GitHub
parent e80bede7b7
commit 2594c1edb1
3 changed files with 48 additions and 32 deletions

View File

@ -1,10 +1,8 @@
import * as fs from 'fs';
import { createWriteStream } from 'fs';
import * as path from 'path';
import { promisify } from 'util';
import { GitHubRelease, GitHubReleaseAsset } from '../utils/checkForUpdatesUtils';
import { downloadFile } from '../../tools/tool-utils';
const pipeline = promisify(require('stream').pipeline);
export interface Context {
repo: string; // {owner}/{repo}
@ -14,6 +12,7 @@ export interface Context {
const apiBaseUrl = 'https://api.github.com/repos/';
const defaultApiHeaders = (context: Context) => ({
'User-Agent': 'Joplin',
'Authorization': `token ${context.githubToken}`,
'X-GitHub-Api-Version': '2022-11-28',
'Accept': 'application/vnd.github+json',
@ -45,7 +44,7 @@ export const getTargetRelease = async (context: Context, targetTag: string): Pro
};
// Download a file from Joplin Desktop releases
export const downloadFile = async (context: Context, asset: GitHubReleaseAsset, destinationDir: string): Promise<string> => {
export const downloadFileFromGitHub = async (context: Context, asset: GitHubReleaseAsset, destinationDir: string) => {
const downloadPath = path.join(destinationDir, asset.name);
if (!fs.existsSync(destinationDir)) {
fs.mkdirSync(destinationDir);
@ -53,19 +52,10 @@ export const downloadFile = async (context: Context, asset: GitHubReleaseAsset,
/* eslint-disable no-console */
console.log(`Downloading ${asset.name} from ${asset.url} to ${downloadPath}`);
const response = await fetch(asset.url, {
headers: {
...defaultApiHeaders(context),
'Accept': 'application/octet-stream',
},
await downloadFile(asset.url, downloadPath, {
...defaultApiHeaders(context),
'Accept': 'application/octet-stream',
});
if (!response.ok) {
throw new Error(`Failed to download file: Status Code ${response.status}`);
}
const fileStream = createWriteStream(downloadPath);
await pipeline(response.body, fileStream);
console.log('Download successful!');
/* eslint-enable no-console */
return downloadPath;
};