1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-26 22:41:17 +02:00

Api: Add capability of limiting downloads (#9788)

This commit is contained in:
pedr
2024-03-09 07:45:21 -03:00
committed by GitHub
parent 4d8fcff6d5
commit 17a8ce5010
9 changed files with 152 additions and 22 deletions

View File

@@ -9,6 +9,7 @@ import * as fs from 'fs-extra';
import * as pdfJsNamespace from 'pdfjs-dist';
import { writeFile } from 'fs/promises';
import { ResourceEntity } from './services/database/types';
import { DownloadController } from './downloadController';
import { TextItem } from 'pdfjs-dist/types/src/display/api';
import replaceUnsupportedCharacters from './utils/replaceUnsupportedCharacters';
@@ -25,6 +26,15 @@ const dgram = require('dgram');
const proxySettings: any = {};
type FetchBlobOptions = {
path?: string;
method?: string;
maxRedirects?: number;
timeout?: number;
headers?: any;
downloadController?: DownloadController;
};
function fileExists(filePath: string) {
try {
return fs.statSync(filePath).isFile();
@@ -493,7 +503,7 @@ function shimInit(options: ShimInitOptions = null) {
}, options);
};
shim.fetchBlob = async function(url: any, options) {
shim.fetchBlob = async function(url: any, options: FetchBlobOptions) {
if (!options || !options.path) throw new Error('fetchBlob: target file path is missing');
if (!options.method) options.method = 'GET';
// if (!('maxRetry' in options)) options.maxRetry = 5;
@@ -510,6 +520,7 @@ function shimInit(options: ShimInitOptions = null) {
const http = url.protocol.toLowerCase() === 'http:' ? require('follow-redirects').http : require('follow-redirects').https;
const headers = options.headers ? options.headers : {};
const filePath = options.path;
const downloadController = options.downloadController;
function makeResponse(response: any) {
return {
@@ -571,6 +582,11 @@ function shimInit(options: ShimInitOptions = null) {
});
const request = http.request(requestOptions, (response: any) => {
if (downloadController) {
response.on('data', downloadController.handleChunk(request));
}
response.pipe(file);
const isGzipped = response.headers['content-encoding'] === 'gzip';