4 Replies Latest reply on Feb 1, 2017 12:36 PM by flingo

    LrHttp.post(): Using file:read() instead of LrFileUtils.readFile() doesn't work

    flingo Level 1

      I'm using Lr 6.8 / Win10.

      I have running code that uploads photo/videos to a WebService via LrHttp.post(). Basicly, the code is:

      function PSUploadAPI.uploadPictureFile(...)

      ...

           local respBody, respHeaders = LrHttp.post(h.serverUrl .. h.uploadPath, LrFileUtils.readFile(srcFilename), postHeaders, 'POST', timeout, fileSize)

      ...

      Now, someone tried to upload a 11 minute FullHD/30fps AVCHD Video with a filesize of 2GB and ended up with:

      Internal error: 'Attempt to seek past end of file'

      So, I assume there is a size limit for LrFileUtils.readFile(). Looking for a solution I found this thread answered by John:

      How to avoid LrStringUtils.encodeBase64 and LrFileUtils.readFile to block the main thread?

       

      I reworked the code accordingly to use file:read():

      local readFiles = {}

       

      local function openFile(filename)

          readFiles[filename] = io.open(filename, "rb")

      end

         

      local function readFileChunk(filename, nBytes)

          if not readFiles[filename] then

              writeLogfile(4, string.format("readFileChunk(%s, %d): file not opened\n", filename, nBytes))

              return false

          end

       

          local readBuf = readFiles[filename]:read(nBytes)

          if readBuf then

              writeLogfile(4, string.format("readFileChunk(%s, %d) returns %d Bytes\n", filename, nBytes, #readBuf))

          else

              writeLogfile(4, string.format("readFileChunk(%s, %d) returns EOF\n", filename, nBytes))

          endI

          return readBuf

      end

       

      local function closeFile(filename)

          io.close(readFiles[filename])

          readFiles[filename] = nil

      end

       

      function PSUploadAPI.uploadPictureFile(...)

      ...

          openFile(srcFilename)

          local respBody, respHeaders = LrHttp.post(h.serverUrl .. h.uploadPath, readFileChunk(srcFilename, 10000), postHeaders, 'POST', timeout, fileSize)

          closeFile(srcFilename)

       

      I could verify that readFileChunk() works as expected (from LrHttp.post()), returning chunks of max 10.000 Bytes until EOF, when it returns nil. But for some reason LrHttp.post() is calling the callback function only once.

       

      Questions:

      • Can anybody confirm a size limitation in LrFileUtils.fileRead() somewhere below 2GB?
      • Is anybody using LrHttp.post() with a callback function other than LrFileUtils.fileRead() successfully?

       

      Thanks, Martin