0

I'm trying to read from a file at a certain position for a certain length.

The code reads the first few chunks (varies each time) but getFileOrNull sometimes returns errors such as -1410303 or -894643 I'm not sure what these error numbers mean. having this issue on android. file sizes during testing are 4.9MBs and 4.4MBs

// gets base64 data from a file at lastPosition of a given length or null on error
const getFileOrNull = async (fileURLX, lastPosition, length, tempname, chunkIndex, totalChunks):Promise<string | null> =>{
    return (await FileSystem.readAsStringAsync(fileURLX, {
      encoding: FileSystem.EncodingType.Base64,
      position: lastPosition,
      length: length
    }).then( x => x).catch( er => {
      console.log(tempname + ", forloop: " + chunkIndex.toString() + "/" + totalChunks.toString(), er);
      return null;
    }));
  }
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
const fileURL = "/myfilestoredonthedevice.mp4"; // this points to a file on the device, which i have access to
const fileinfo: FileSystem.FileInfo = await FileSystem.getInfoAsync(fileURL);
const chunkSize:number = 100000; // chunks need to be 100KBs
const totalChunks = Math.ceil((fileinfo.size ?? 0) / chunkSize); // calculate total chunks based on file size
let lastPosition = 0; //track where we are in the file
let merge:boolean = false; // used to mark that we are on the last chunk
let fileURL2:string = "";
let tempname = "mycustomfilename.mp4";

// loop through and upload each chunk
for(let chunkIndex=0; chunkIndex < totalChunks; chunkIndex++){
    if(chunkIndex === (totalChunks - 1)){merge = true;}
    lastPosition += chunkIndex * chunkSize;
    length = chunkSize;
    if(lastPosition + chunkSize > (fileinfo.size ?? 0)){
      length = (fileinfo.size ?? 0) - lastPosition;
    }
    let data:any = null; // store data as null
    while(data == null){ // loop untill you get valid data (this gets stuck)
      data = await getFileOrNull(fileURL, lastPosition, length, tempname, chunkIndex, totalChunks);
    }
    // this section below works fine, issue is above this line....
    uploadFileName = (await Helper.uploadData(
      data,
    ).then( x => x).catch( er => {
      return null;
    }));
    if(chunkIndex === (totalChunks - 1)){
        fileURL2 = uploadFileName;
        break;
      }
    }
  }
Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28

0 Answers0