|
24 | 24 | */ |
25 | 25 |
|
26 | 26 | import * as RNFS from '@dr.pogodin/react-native-fs'; |
| 27 | +import { Platform } from 'react-native'; |
27 | 28 | import { RNEDirectory } from './constants/directories'; |
28 | 29 | import { |
29 | 30 | ResourceSource, |
@@ -71,59 +72,83 @@ class BareResourceFetcherClass extends BaseResourceFetcherClass<ActiveDownload> |
71 | 72 | } |
72 | 73 |
|
73 | 74 | protected async pause(source: ResourceSource): Promise<void> { |
74 | | - const dl = this.downloads.get(source)!; |
75 | | - if (dl.status === DownloadStatus.PAUSED) { |
| 75 | + const downloadHandle = this.downloads.get(source)!; |
| 76 | + if (downloadHandle.status === DownloadStatus.PAUSED) { |
76 | 77 | throw new RnExecutorchError( |
77 | 78 | RnExecutorchErrorCode.ResourceFetcherAlreadyPaused, |
78 | 79 | "The file download is currently paused. Can't pause the download of the same file twice." |
79 | 80 | ); |
80 | 81 | } |
81 | | - if (!dl.task) { |
| 82 | + if (Platform.OS === 'android') { |
82 | 83 | throw new RnExecutorchError( |
83 | 84 | RnExecutorchErrorCode.ResourceFetcherPlatformNotSupported, |
84 | 85 | 'Pause is not supported on Android. Use cancelFetching and re-fetch instead.' |
85 | 86 | ); |
86 | 87 | } |
87 | | - dl.status = DownloadStatus.PAUSED; |
88 | | - dl.task.pause(); |
| 88 | + if (!downloadHandle.task) { |
| 89 | + throw new RnExecutorchError( |
| 90 | + RnExecutorchErrorCode.ResourceFetcherDownloadFailed, |
| 91 | + 'Download task is missing. This should not happen on iOS.' |
| 92 | + ); |
| 93 | + } |
| 94 | + downloadHandle.status = DownloadStatus.PAUSED; |
| 95 | + downloadHandle.task.pause(); |
89 | 96 | } |
90 | 97 |
|
91 | 98 | protected async resume(source: ResourceSource): Promise<void> { |
92 | | - const dl = this.downloads.get(source)!; |
93 | | - if (dl.status === DownloadStatus.ONGOING) { |
| 99 | + const downloadHandle = this.downloads.get(source)!; |
| 100 | + if (downloadHandle.status === DownloadStatus.ONGOING) { |
94 | 101 | throw new RnExecutorchError( |
95 | 102 | RnExecutorchErrorCode.ResourceFetcherAlreadyOngoing, |
96 | 103 | "The file download is currently ongoing. Can't resume the ongoing download." |
97 | 104 | ); |
98 | 105 | } |
99 | | - if (!dl.task) { |
| 106 | + if (Platform.OS === 'android') { |
100 | 107 | throw new RnExecutorchError( |
101 | 108 | RnExecutorchErrorCode.ResourceFetcherPlatformNotSupported, |
102 | 109 | 'Resume is not supported on Android. Use fetch to restart the download.' |
103 | 110 | ); |
104 | 111 | } |
| 112 | + if (!downloadHandle.task) { |
| 113 | + throw new RnExecutorchError( |
| 114 | + RnExecutorchErrorCode.ResourceFetcherDownloadFailed, |
| 115 | + 'Download task is missing. This should not happen on iOS.' |
| 116 | + ); |
| 117 | + } |
105 | 118 | // Set status back to ONGOING before resuming so the .done() callback |
106 | 119 | // registered in handleRemote knows it's safe to proceed (not paused/canceled). |
107 | | - dl.status = DownloadStatus.ONGOING; |
108 | | - dl.task.resume(); |
| 120 | + downloadHandle.status = DownloadStatus.ONGOING; |
| 121 | + downloadHandle.task.resume(); |
109 | 122 | } |
110 | 123 |
|
111 | 124 | protected async cancel(source: ResourceSource): Promise<void> { |
112 | | - const dl = this.downloads.get(source)!; |
113 | | - |
114 | | - if (dl.task) { |
115 | | - // iOS: background downloader cancel |
116 | | - dl.task.stop(); |
117 | | - } else if (dl.jobId !== undefined) { |
118 | | - // Android: RNFS cancel + cleanup of partial file |
119 | | - RNFS.stopDownload(dl.jobId); |
120 | | - if (await ResourceFetcherUtils.checkFileExists(dl.cacheFileUri)) { |
121 | | - await RNFS.unlink(dl.cacheFileUri); |
| 125 | + const downloadHandle = this.downloads.get(source)!; |
| 126 | + |
| 127 | + if (Platform.OS === 'ios') { |
| 128 | + if (!downloadHandle.task) { |
| 129 | + throw new RnExecutorchError( |
| 130 | + RnExecutorchErrorCode.ResourceFetcherDownloadFailed, |
| 131 | + 'Download task is missing. This should not happen on iOS.' |
| 132 | + ); |
| 133 | + } |
| 134 | + downloadHandle.task.stop(); |
| 135 | + } else if (Platform.OS === 'android') { |
| 136 | + if (downloadHandle.jobId === undefined) { |
| 137 | + throw new RnExecutorchError( |
| 138 | + RnExecutorchErrorCode.ResourceFetcherDownloadFailed, |
| 139 | + 'Download job ID is missing. This should not happen on Android.' |
| 140 | + ); |
| 141 | + } |
| 142 | + RNFS.stopDownload(downloadHandle.jobId); |
| 143 | + if ( |
| 144 | + await ResourceFetcherUtils.checkFileExists(downloadHandle.cacheFileUri) |
| 145 | + ) { |
| 146 | + await RNFS.unlink(downloadHandle.cacheFileUri); |
122 | 147 | } |
123 | 148 | } |
124 | 149 |
|
125 | 150 | this.downloads.delete(source); |
126 | | - dl.reject( |
| 151 | + downloadHandle.reject( |
127 | 152 | new RnExecutorchError( |
128 | 153 | RnExecutorchErrorCode.DownloadInterrupted, |
129 | 154 | 'Download was canceled.' |
|
0 commit comments