From 39a8948976d7d9e113c8578d4298c5926af4ddac Mon Sep 17 00:00:00 2001 From: Helmut Date: Thu, 28 May 2026 07:00:40 +0200 Subject: [PATCH] feat(ui): add iOS Web Share button next to download link MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a share button to the completed-list action row that hands the downloaded file off to the platform share sheet via navigator.share(). On iOS Safari/Chrome this surfaces the native Save-to-Photos / Save-to- Files / AirDrop options for videos and images, and Files / 3rd-party app targets for audio. On platforms without Web Share support (Desktop Firefox/Chrome/Safari) the button hides itself; the existing download link remains the universal fallback. Implementation notes: - canShareDownloads() requires both navigator.share AND navigator.canShare (Desktop Safari has the former without the latter; we always intend to share a file, not a URL) - shareDownload() fetches the file via the existing buildDownloadLink() helper, wraps it in a File, then runs canShare() before share() so we can bail out cleanly on platforms that reject the MIME type - AbortError (user dismisses sheet) is silenced; other errors logged - Tooltip on the button explains the iOS behaviour briefly Refs alexta69/metube#582 — addresses the 'add to Photos.app' request without depending on the iOS Shortcut, which has had reliability issues (cf #763). --- ui/src/app/app.html | 3 +++ ui/src/app/app.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/ui/src/app/app.html b/ui/src/app/app.html index b2ae0db..26f496a 100644 --- a/ui/src/app/app.html +++ b/ui/src/app/app.html @@ -854,6 +854,9 @@ @if (entry[1].filename) { } + @if (entry[1].filename && canShareDownloads()) { + + } diff --git a/ui/src/app/app.ts b/ui/src/app/app.ts index a73f879..03448ee 100644 --- a/ui/src/app/app.ts +++ b/ui/src/app/app.ts @@ -7,7 +7,7 @@ import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { FontAwesomeModule } from '@fortawesome/angular-fontawesome'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { NgSelectModule } from '@ng-select/ng-select'; -import { faTrashAlt, faCheckCircle, faTimesCircle, faRedoAlt, faSun, faMoon, faCheck, faCircleHalfStroke, faDownload, faExternalLinkAlt, faFileImport, faFileExport, faCopy, faClock, faTachometerAlt, faSortAmountDown, faSortAmountUp, faChevronRight, faChevronDown, faUpload, faPause, faPlay } from '@fortawesome/free-solid-svg-icons'; +import { faTrashAlt, faCheckCircle, faTimesCircle, faRedoAlt, faSun, faMoon, faCheck, faCircleHalfStroke, faDownload, faExternalLinkAlt, faFileImport, faFileExport, faCopy, faClock, faTachometerAlt, faSortAmountDown, faSortAmountUp, faChevronRight, faChevronDown, faUpload, faPause, faPlay, faShareNodes } from '@fortawesome/free-solid-svg-icons'; import { faGithub } from '@fortawesome/free-brands-svg-icons'; import { CookieService } from 'ngx-cookie-service'; import { AddDownloadPayload, DownloadsService } from './services/downloads.service'; @@ -185,6 +185,7 @@ export class App implements AfterViewInit, OnInit, OnDestroy { faUpload = faUpload; faPause = faPause; faPlay = faPlay; + faShareNodes = faShareNodes; subtitleLanguages = [ { id: 'en', text: 'English' }, { id: 'ar', text: 'Arabic' }, @@ -1189,6 +1190,51 @@ export class App implements AfterViewInit, OnInit, OnDestroy { return baseDir + encodeURIComponent(download.filename); } + // Web Share API support — primarily for iOS Safari / Chrome, lets the user + // hand the downloaded file off to the platform share sheet (Photos.app, + // Files, third-party apps, AirDrop). Falls back silently to the standard + // download flow on platforms without navigator.share / canShare. + canShareDownloads(): boolean { + // navigator.share alone is not enough — Desktop Safari implements + // navigator.share but not canShare with files. We explicitly require + // both, since we always intend to share a file (not a URL). + return typeof navigator !== 'undefined' + && typeof navigator.share === 'function' + && typeof navigator.canShare === 'function'; + } + + async shareDownload(download: Download): Promise { + if (!this.canShareDownloads()) { + return; + } + try { + const response = await fetch(this.buildDownloadLink(download)); + if (!response.ok) { + throw new Error(`HTTP ${response.status} fetching file for share`); + } + const blob = await response.blob(); + const file = new File([blob], download.filename, { + type: blob.type || 'application/octet-stream', + }); + const payload: ShareData = { files: [file], title: download.title }; + if (!navigator.canShare(payload)) { + // File type not shareable on this platform (e.g. desktop browsers, + // or some MIME types iOS refuses). Bail out so the user can still + // use the regular download button right next to this one. + console.warn('navigator.canShare rejected payload for', download.filename); + return; + } + await navigator.share(payload); + } catch (err: any) { + // AbortError = user dismissed the share sheet → silent no-op. + // Other errors (network, file too big, …) get logged but we don't + // surface a UI error: the regular download link remains a fallback. + if (err?.name !== 'AbortError') { + console.error('Share failed:', err); + } + } + } + buildResultItemTooltip(download: Download) { const parts = []; if (download.msg) {