1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00

Clipper: Fix process stopping because one resource can't be created (#10337)

This commit is contained in:
pedr 2024-04-25 09:53:34 -03:00 committed by GitHub
parent 296b60800a
commit 4e95486c5c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 38 additions and 28 deletions

View File

@ -151,12 +151,13 @@ describe('routes/notes', () => {
}); });
test('should not create resource from files that does not exist', async () => { test('should not create resource from files that does not exist', async () => {
expect( Logger.globalLogger.enabled = false;
async () => createResourcesFromPaths([ const result = await createResourcesFromPaths([
{ originalUrl: 'not-a-real-file', path: '/does/not/exist' }, { originalUrl: 'not-a-real-file', path: '/does/not/exist' },
]), ]);
).rejects.toThrow('Cannot access /does/not/exist'); Logger.globalLogger.enabled = true;
expect(result[0].resource).toBe(null);
const resources = await Resource.all(); const resources = await Resource.all();
expect(resources.length).toBe(0); expect(resources.length).toBe(0);
}); });
@ -173,4 +174,14 @@ describe('routes/notes', () => {
expect(await Note.load(note2.id)).toBeFalsy(); expect(await Note.load(note2.id)).toBeFalsy();
}); });
test('should not stop execution if a file can not be processed', async () => {
Logger.globalLogger.enabled = false;
const result = await createResourcesFromPaths([
{ originalUrl: 'asdf.png', path: `${__dirname}/bad-path-should-not-exist` },
{ originalUrl: 'asdf.png', path: `${__dirname}/../../../images/SideMenuHeader.png` },
]);
Logger.globalLogger.enabled = true;
expect(result.length).toBe(2);
});
}); });

View File

@ -29,6 +29,7 @@ const { ErrorNotFound } = require('../utils/errors');
import { fileUriToPath } from '@joplin/utils/url'; import { fileUriToPath } from '@joplin/utils/url';
import { NoteEntity, ResourceEntity } from '../../database/types'; import { NoteEntity, ResourceEntity } from '../../database/types';
import { DownloadController } from '../../../downloadController'; import { DownloadController } from '../../../downloadController';
import { FetchBlobOptions } from '../../../types';
const logger = Logger.create('routes/notes'); const logger = Logger.create('routes/notes');
@ -68,11 +69,11 @@ type RequestNote = {
stylesheets: any; stylesheets: any;
}; };
type FetchOptions = { interface FetchOptions extends FetchBlobOptions {
timeout?: number; timeout?: number;
maxRedirects?: number; maxRedirects?: number;
downloadController?: DownloadController; downloadController?: DownloadController;
}; }
type DownloadedMediaFile = { type DownloadedMediaFile = {
@ -80,7 +81,7 @@ type DownloadedMediaFile = {
path: string; path: string;
}; };
interface ResourceFromPath extends DownloadedMediaFile { export interface ResourceFromPath extends DownloadedMediaFile {
resource: ResourceEntity; resource: ResourceEntity;
} }
@ -315,20 +316,17 @@ async function downloadMediaFiles(urls: string[], fetchOptions?: FetchOptions, a
} }
export async function createResourcesFromPaths(mediaFiles: DownloadedMediaFile[]) { export async function createResourcesFromPaths(mediaFiles: DownloadedMediaFile[]) {
const resources: Promise<ResourceFromPath>[] = []; const processFile = async (mediaFile: DownloadedMediaFile) => {
for (const mediaFile of mediaFiles) {
try { try {
resources.push( const resource = await shim.createResourceFromPath(mediaFile.path);
shim.createResourceFromPath(mediaFile.path) return { ...mediaFile, resource };
// eslint-disable-next-line
.then(resource => ({ ...mediaFile, resource }))
);
} catch (error) { } catch (error) {
logger.warn(`Cannot create resource for ${mediaFile.originalUrl}`, error); logger.warn(`Cannot create resource for ${mediaFile.originalUrl}`, error);
return { ...mediaFile, resource: null };
} }
} };
const resources = mediaFiles.map(processFile);
return Promise.all(resources); return Promise.all(resources);
} }

View File

@ -9,9 +9,9 @@ import * as fs from 'fs-extra';
import * as pdfJsNamespace from 'pdfjs-dist'; import * as pdfJsNamespace from 'pdfjs-dist';
import { writeFile } from 'fs/promises'; import { writeFile } from 'fs/promises';
import { ResourceEntity } from './services/database/types'; import { ResourceEntity } from './services/database/types';
import { DownloadController } from './downloadController';
import { TextItem } from 'pdfjs-dist/types/src/display/api'; import { TextItem } from 'pdfjs-dist/types/src/display/api';
import replaceUnsupportedCharacters from './utils/replaceUnsupportedCharacters'; import replaceUnsupportedCharacters from './utils/replaceUnsupportedCharacters';
import { FetchBlobOptions } from './types';
import FileApiDriverLocal from './file-api-driver-local'; import FileApiDriverLocal from './file-api-driver-local';
const mimeUtils = require('./mime-utils.js').mime; const mimeUtils = require('./mime-utils.js').mime;
@ -27,16 +27,6 @@ const dgram = require('dgram');
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
const proxySettings: any = {}; const proxySettings: any = {};
type FetchBlobOptions = {
path?: string;
method?: string;
maxRedirects?: number;
timeout?: number;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
headers?: any;
downloadController?: DownloadController;
};
function fileExists(filePath: string) { function fileExists(filePath: string) {
try { try {
return fs.statSync(filePath).isFile(); return fs.statSync(filePath).isFile();

View File

@ -1,3 +1,5 @@
import { DownloadController } from './downloadController';
export enum ApplicationPlatform { export enum ApplicationPlatform {
Unknown = 0, Unknown = 0,
Windows = 1, Windows = 1,
@ -13,3 +15,12 @@ export enum ApplicationType {
Mobile = 2, Mobile = 2,
Cli = 3, Cli = 3,
} }
export type FetchBlobOptions = {
path?: string;
method?: string;
maxRedirects?: number;
timeout?: number;
headers?: Record<string, string>;
downloadController?: DownloadController;
};