triniwiz / nativescript-downloader

Apache License 2.0
32 stars 18 forks source link

IOS Cannot find downloaded files, but works fine on Android #56

Closed speti43 closed 1 year ago

speti43 commented 1 year ago

Code:


 const url = `${environment.streaming.baseUrl}/api/download/${this.inputItem.articleNumber}/${playlistitemid}`;
      console.log('starting download', url);
      const cookie = await this.auth();
      const headerHttp = {
        Cookie: cookie[0]
      };

      const downloader = new Downloader();
      const filePathArray = this.getChapterLocalPathArray(trackOrder);
      const imageDownloaderId = downloader.createDownload({
        url,
        path: filePathArray[0],
        fileName: filePathArray[1],
        headers: headerHttp
      });
      await downloader
        .start(imageDownloaderId, (progressData: ProgressEventData) => {
          console.log(`Progress : ${progressData.value}%`);
          console.log(`Current Size : ${progressData.currentSize}%`);
          console.log(`Total Size : ${progressData.totalSize}%`);
          console.log(`Download Speed in bytes : ${progressData.speed}%`);
        })
        .then(response => {
          if (response.status === 'completed') {
            // Runs correctly on both platforms, but something is not ok under the hood on IOS, because the files cannot be found.
            console.log('DOWNLOAD SUCCESS');
            this.audioBookChapterDbService.updateChapterDownloaded(this.inputItem.dbId, chapterNumber, true);
          } else {
            console.log('ERROR DURING DOWNLOAD', response.status);
            console.log(response.message.toString());
          }
        });

// This returns false on ios:
File.exists(filePath)

Filepath is correct, same code runs fine on android, file is downloaded and can be found.

Which platform(s) does your issue occur on?

IPhone 14 IOS 16.2 Andoid 13 Google Pixel 6

UPDATE:

If I don't use these parameters:

  path: filePathArray[0],
   fileName: filePathArray[1],

Then it works fine on IOS too.

speti43 commented 1 year ago

Problem solved: We need to create the folder on IOS. So the final code:

  const url = `${environment.streaming.baseUrl}/api/download/${this.inputItem.articleNumber}/${playlistitemid}`;
      console.log('starting download', url);
      const cookie = await this.auth();
      const headerHttp = {
        Cookie: cookie[0]
      };
      const pathAndFileName = this.getChapterLocalPathObject(trackOrder);

      //create folder if not exists (IOS will crash without this!)
      Folder.fromPath(pathAndFileName.path);

      const imageDownloaderId = this.downloader.createDownload({
        url,
        headers: headerHttp,
        path: pathAndFileName.path,
        fileName: pathAndFileName.fileName
      });

      const response = await this.downloader.start(imageDownloaderId, (progressData: ProgressEventData) => {
        console.log(`Progress : ${progressData.value}%`);
        console.log(`Current Size : ${progressData.currentSize}%`);
        console.log(`Total Size : ${progressData.totalSize}%`);
        console.log(`Download Speed in bytes : ${progressData.speed}%`);
      });

      if (response.status === 'completed') {
        console.log('DOWNLOAD SUCCESS');
        await this.audioBookChapterDbService.updateChapterDownloaded(this.inputItem.dbId, chapterNumber, true);
        console.log('DOWNLOAD COMPLETE');
        return true;
      } else {
        console.log('ERROR DURING DOWNLOAD', response.status);
        console.log(response.message.toString());
      }