kesha-antonov / react-native-background-downloader

About A library for React-Native to help you download large files on iOS and Android both in the foreground and most importantly in the background.
https://www.npmjs.com/package/@kesha-antonov/react-native-background-downloader
Other
49 stars 7 forks source link

How to fetch downloaded video #23

Open ErMapsh opened 3 months ago

ErMapsh commented 3 months ago

- Using README.MD, I save videos(files) to directories.documents directory, now I have the challenge of fetching downloaded videos with metadata and play, how can I do it? @kesha-antonov

const downloadVideo = async data => {
    try {
      if (downloadState == 'DOWNLOAD') {
        const url = data.videoUrl;
        const jobId = String(data.id);
        let task = download({
          id: jobId,
          url: url,
          destination: `${directories.documents}/${jobId}.mp4`,
          metadata: data,
        })
          .begin(({expectedBytes, headers}) => {
            console.log(`Going to download ${expectedBytes} bytes!`);
            setDownloadState('DOWNLOADING');
          })
          .progress(({bytesDownloaded, bytesTotal}) => {
            console.log(`Downloaded: ${(bytesDownloaded / bytesTotal) * 100}%`);
            setDownloadState('DOWNLOADING');
          })
          .done(async ({bytesDownloaded, bytesTotal}) => {
            setDownloadState('DONE');
            console.log('Download is done!', {bytesDownloaded, bytesTotal});
            let res = await completeHandler(jobId);
            console.log('res', res);
          })
          .error(({error, errorCode}) => {
            Toast.show({
              type: ALERT_TYPE.DANGER,
              title: 'Error',
              textBody: 'Try again Later',
              button: 'close',
              closeOnOverlayTap: false,
              onPressButton: () => {
                Toast.hide();
              },
            });
            console.log('Download canceled due to error: ', {error, errorCode});
          });
      } else if (downloadState == 'DOWNLOADING' || downloadState == 'PAUSED') {
        /* open modal from bottom */
        console.log('Opening modal');
      } else if (downloadState == 'DONE') {
        /* open dialog box */
        console.log('Dialog box');
      }
    } catch (error) {
      console.log(error);
    }
  };

Need another enhancement if possible

  1. resume and pause in both platform, not able to pause in android
ErMapsh commented 2 months ago
    // https://android-review.googlesource.com/c/platform/packages/providers/DownloadProvider/+/2089866
    public void pauseDownload(long downloadId) {
        // ContentValues values = new ContentValues();

        // values.put(Downloads.Impl.COLUMN_CONTROL, Downloads.Impl.CONTROL_PAUSED);
        // values.put(Downloads.Impl.COLUMN_STATUS,
        // Downloads.Impl.STATUS_PAUSED_BY_APP);

        // downloadManager.mResolver.update(ContentUris.withAppendedId(mBaseUri,
        // ids[0]), values, null, null)
    }

    public void resumeDownload(long downloadId) {
        // ContentValues values = new ContentValues();

        // values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_PENDING);
        // values.put(Downloads.Impl.COLUMN_CONTROL, Downloads.Impl.CONTROL_RUN);
    } 

I'm looking to resolve this issue but I can't find the way to fix the issue, can u look out this @kesha-antonov

kesha-antonov commented 2 months ago

Hey man Overwhelmed with work rn It's open source, feel free to improve code & propose PR

ErMapsh commented 2 months ago

Hey man Overwhelmed with work rn It's open source, feel free to improve code & propose PR

sure, why not I will try