fix(ui): mitigate iOS webkit upload bug (#2738)
This commit is contained in:
@@ -262,7 +262,7 @@ describe('BookFileService', () => {
|
||||
expect(request.request.method).toBe('POST');
|
||||
|
||||
const formData = request.request.body as FormData;
|
||||
expect(formData.get('file')).toBe(upload);
|
||||
expect(formData.get('file')).not.toBeNull();
|
||||
expect(formData.get('isBook')).toBe('true');
|
||||
expect(formData.get('bookType')).toBe('EPUB');
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {Observable, throwError, from} from 'rxjs';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {catchError, tap} from 'rxjs/operators';
|
||||
import {HttpClient, HttpEventType, HttpRequest} from '@angular/common/http';
|
||||
import {catchError, filter, map, tap} from 'rxjs/operators';
|
||||
import {AdditionalFile, AdditionalFileType, Book, DetachBookFileResponse, DuplicateDetectionRequest, DuplicateGroup} from '../model/book.model';
|
||||
import {API_CONFIG} from '../../../core/config/api-config';
|
||||
import {MessageService} from '@openng/optimus-ui/api';
|
||||
@@ -11,6 +11,7 @@ import {LocalSettingsService} from '../../../shared/service/local-settings.servi
|
||||
import {TranslocoService} from '@jsverse/transloco';
|
||||
import {QueryClient} from '@tanstack/angular-query-experimental';
|
||||
import {patchAttachedBookFilesInCache, patchBookInCacheWith, upsertBooksInCache} from './legacy-book-cache';
|
||||
import {mitigateWebkitUploadBug} from '../../../shared/util/mitigate-webkit-upload-bug';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
@@ -131,8 +132,15 @@ export class BookFileService {
|
||||
formData.append('bookType', bookType);
|
||||
}
|
||||
}
|
||||
return this.http.post<AdditionalFile>(`${this.url}/${bookId}/files`, formData).pipe(
|
||||
tap(newFile => {
|
||||
|
||||
const req = mitigateWebkitUploadBug(
|
||||
new HttpRequest('POST', `${this.url}/${bookId}/files`, formData)
|
||||
);
|
||||
|
||||
return this.http.request<AdditionalFile>(req)
|
||||
.pipe(filter(e => e.type == HttpEventType.Response))
|
||||
.pipe(map(e => e.body!))
|
||||
.pipe(tap(newFile => {
|
||||
patchBookInCacheWith(this.queryClient, bookId, book => {
|
||||
if (fileType === AdditionalFileType.ALTERNATIVE_FORMAT) {
|
||||
return {...book, alternativeFormats: [...(book.alternativeFormats || []), newFile]};
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {Observable} from 'rxjs';
|
||||
import {HttpClient, HttpParams} from '@angular/common/http';
|
||||
import {catchError, map, tap} from 'rxjs/operators';
|
||||
import {Observable, of} from 'rxjs';
|
||||
import {HttpClient, HttpEventType, HttpParams, HttpRequest} from '@angular/common/http';
|
||||
import {catchError, filter, map, tap, mergeMap} from 'rxjs/operators';
|
||||
import {Book, BookMetadata, BulkMetadataUpdateRequest, MetadataUpdateWrapper} from '../model/book.model';
|
||||
import {API_CONFIG} from '../../../core/config/api-config';
|
||||
import {MessageService} from '@openng/optimus-ui/api';
|
||||
import {TranslocoService} from '@jsverse/transloco';
|
||||
import {QueryClient} from '@tanstack/angular-query-experimental';
|
||||
import {invalidateAllBookCaches, invalidateBooksById, patchBookMetadataInCache, patchBooksInCacheWith} from './legacy-book-cache';
|
||||
import {mitigateWebkitUploadBug} from '../../../shared/util/mitigate-webkit-upload-bug';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
@@ -164,11 +165,19 @@ export class BookMetadataManageService {
|
||||
uploadAudiobookCoverFromFile(bookId: number, file: File): Observable<void> {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
return this.http.post<void>(`${this.url}/${bookId}/metadata/audiobook-cover/upload`, formData).pipe(
|
||||
tap(() => {
|
||||
invalidateBooksById(this.queryClient, [bookId]);
|
||||
})
|
||||
|
||||
const req = mitigateWebkitUploadBug(
|
||||
new HttpRequest('POST', `${this.url}/${bookId}/metadata/audiobook-cover/upload`, formData)
|
||||
);
|
||||
|
||||
return this.http.request<void>(req)
|
||||
.pipe(filter(e => e.type == HttpEventType.Response))
|
||||
.pipe(mergeMap(() => of<void>(void 0)))
|
||||
.pipe(
|
||||
tap(() => {
|
||||
invalidateBooksById(this.queryClient, [bookId]);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
getUploadAudiobookCoverUrl(bookId: number): string {
|
||||
@@ -202,10 +211,18 @@ export class BookMetadataManageService {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('bookIds', bookIds.join(','));
|
||||
return this.http.post<void>(`${this.url}/bulk-upload-cover`, formData).pipe(
|
||||
tap(() => {
|
||||
invalidateBooksById(this.queryClient, bookIds);
|
||||
})
|
||||
|
||||
const req = mitigateWebkitUploadBug(
|
||||
new HttpRequest('POST', `${this.url}/bulk-upload-cover`, formData)
|
||||
);
|
||||
|
||||
return this.http.request<void>(req)
|
||||
.pipe(filter(e => e.type == HttpEventType.Response))
|
||||
.pipe(mergeMap(() => of<void>(void 0)))
|
||||
.pipe(
|
||||
tap(() => {
|
||||
invalidateBooksById(this.queryClient, bookIds);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import {SelectButton} from '@openng/optimus-ui/selectbutton';
|
||||
import {DynamicDialogRef} from '@openng/optimus-ui/dynamicdialog';
|
||||
import {ProgressBar} from '@openng/optimus-ui/progressbar';
|
||||
import {TranslocoDirective, TranslocoService} from '@jsverse/transloco';
|
||||
import {mitigateWebkitUploadBug} from '../../util/mitigate-webkit-upload-bug';
|
||||
|
||||
interface UploadingFile {
|
||||
file: File;
|
||||
@@ -231,9 +232,11 @@ export class BookUploaderComponent {
|
||||
uploadUrl = `${API_CONFIG.BASE_URL}/api/v1/files/upload/bookdrop`;
|
||||
}
|
||||
|
||||
const req = new HttpRequest('POST', uploadUrl, formData, {
|
||||
reportProgress: true
|
||||
});
|
||||
const req = mitigateWebkitUploadBug(
|
||||
new HttpRequest('POST', uploadUrl, formData, { reportProgress: true })
|
||||
);
|
||||
|
||||
mitigateWebkitUploadBug(req);
|
||||
|
||||
this.http.request(req).subscribe({
|
||||
next: (event) => {
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import {computed, signal, effect, inject, Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {HttpClient, HttpEventType, HttpRequest} from '@angular/common/http';
|
||||
import {lastValueFrom, Observable, tap} from 'rxjs';
|
||||
import {CustomFont} from '../model/custom-font.model';
|
||||
import {API_CONFIG} from '../../core/config/api-config';
|
||||
import {AuthService} from './auth.service';
|
||||
import {injectQuery, queryOptions, QueryClient} from '@tanstack/angular-query-experimental';
|
||||
import {mitigateWebkitUploadBug} from '../util/mitigate-webkit-upload-bug';
|
||||
import {filter, map} from 'rxjs/operators';
|
||||
|
||||
const CUSTOM_FONTS_QUERY_KEY = ['customFonts'] as const;
|
||||
|
||||
@@ -59,15 +61,22 @@ export class CustomFontService {
|
||||
formData.append('fontName', fontName);
|
||||
}
|
||||
|
||||
return this.http.post<CustomFont>(`${this.apiUrl}/upload`, formData).pipe(
|
||||
tap(font => {
|
||||
this.queryClient.setQueryData<CustomFont[]>(CUSTOM_FONTS_QUERY_KEY, current =>
|
||||
[...(current ?? []), font]
|
||||
);
|
||||
this.loadFontFace(font).catch(err => {
|
||||
console.error('Failed to load font after upload:', err);
|
||||
});
|
||||
})
|
||||
const req = mitigateWebkitUploadBug(
|
||||
new HttpRequest('POST', `${this.apiUrl}/upload`, formData)
|
||||
);
|
||||
|
||||
return this.http.request<CustomFont>(req)
|
||||
.pipe(filter(e => e.type == HttpEventType.Response))
|
||||
.pipe(map(e => e.body!))
|
||||
.pipe(
|
||||
tap(font => {
|
||||
this.queryClient.setQueryData<CustomFont[]>(CUSTOM_FONTS_QUERY_KEY, current =>
|
||||
[...(current ?? []), font]
|
||||
);
|
||||
this.loadFontFace(font).catch(err => {
|
||||
console.error('Failed to load font after upload:', err);
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import {HttpRequest} from '@angular/common/http';
|
||||
|
||||
const IOS_PLATFORMS = [
|
||||
'iPad Simulator',
|
||||
'iPhone Simulator',
|
||||
'iPod Simulator',
|
||||
'iPad',
|
||||
'iPhone',
|
||||
'iPod'
|
||||
];
|
||||
|
||||
function isIOSWebkit() {
|
||||
return IOS_PLATFORMS.includes(navigator.platform)
|
||||
// iPad on iOS >=13
|
||||
|| (navigator.userAgent.includes("Mac") && "ontouchend" in document)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of an HTTP Request to disable the service worker capture &
|
||||
* copy FormData `File`s for web browser engines which have bugs that prevent
|
||||
* file uploads.
|
||||
*
|
||||
* @see https://bugs.webkit.org/show_bug.cgi?id=319985
|
||||
*
|
||||
* @param request Target request
|
||||
* @return HttpRequest<FormData> Either the original target request, or a mutated request.
|
||||
*/
|
||||
export function mitigateWebkitUploadBug(request: HttpRequest<FormData>): HttpRequest<FormData> {
|
||||
if (isIOSWebkit()) {
|
||||
const body = new FormData();
|
||||
|
||||
if (request.body != null) {
|
||||
// If there is a request body, we need to make a copy of every
|
||||
// file included in the request body via `File.slice`
|
||||
for (const key of request.body.keys()) {
|
||||
const values = request.body.getAll(key);
|
||||
for (const value of values) {
|
||||
if (typeof value == 'string') {
|
||||
body.append(key, value);
|
||||
} else {
|
||||
body.append(key, value.slice(0, value.size, value.type), value.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return request.clone({
|
||||
headers: request.headers.append('ngsw-bypass', '1'),
|
||||
body: request.body ? body : null,
|
||||
});
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
Reference in New Issue
Block a user