qgustavor / mega

Unofficial JavaScript SDK for MEGA
https://mega.js.org/
MIT License
161 stars 43 forks source link

Uploading without encryption returns -4 error #121

Closed Karmel0x closed 2 years ago

Karmel0x commented 2 years ago

Describe the bug

file: undefined
xx\node_modules\megajs\dist\main.node-cjs.js:1505
        returnError(Error("Server returned error " + errorCheck + " while uploading"));
                    ^

Error: Server returned error -4 while uploading
    at checkCallbacks (xx\node_modules\megajs\dist\main.node-cjs.js:1505:21)
    at xx\node_modules\megajs\dist\main.node-cjs.js:1674:15
    at processTicksAndRejections (node:internal/process/task_queues:78:11)

To Reproduce

const { Storage } = require('megajs')
const fs = require('fs')
const crypto = require('crypto')

;(async function () {
  const storage = await new Storage({
    email: 'xx',
    password: 'xx'
  }).ready

  var filename = 'aaa.jpg';
  const fileStream = fs.createReadStream(filename)
  const uploadStream = storage.upload({
    name: filename,
    size: fs.statSync(filename).size,
    uploadCiphertext: true,
    key: crypto.randomBytes(32),
  })
  fileStream.pipe(uploadStream)

  const file = await fileStream.complete
  console.log('file:', file)

}()).catch(error => {
  console.error(error)
  process.exit(1)
})

Expected behavior Upload file

Additional context Probably some changes on MEGA endpoint. I was using older version of this library and it was working some time ago but it has stopped working. On latest version not working too.

qgustavor commented 2 years ago

I don't have time for that now. If someone have time I recommend doing the following steps:

  1. Since the upload implementation in the library uses an old API which for years is not used in the official clients, look for third party clients like megatools and check if they had the same issue and how they fixed it.
  2. Check how MEGA handles uploading now using developer tools. Last time I checked it created a lot of connections which is something I wanted to look and try to understand why but seemed complicated.
  3. Fork the library, clone it locally, make a branch, try to fix the issue and, if you got it, submit a pull request.

I'll probably only have time for it in two weeks or more.

qgustavor commented 2 years ago

I checked megatools out of curiosity: looks like either it does not have this issue or, it it have, it was not been handled yet. Neither the GitHub issue tracker nor the mail archive show reports of this issue. The the commit history don't show recent changes on uploading.

In the other hand the code that handles uploading handles an upload checksum which is something not implemented at the moment. This checksum is quite easy to implement so I suggest someone to try implementing it and checking if it solves this issue. Update this file to something like this:

    const sendChunk = () => {
      const chunkPosition = position
      const chunkBuffer = uploadBuffer
      let tries = 0

      const checksum = Buffer.alloc(12)
      for (let i = 0; i < chunkBuffer.length; i++) {
        checksum[i % 12] ^= chunkBuffer[i]
      }

      const trySendChunk = () => {
        tries++
        this.api.fetch(uploadURL + '/' + (type === 0 ? chunkPosition : (type - 1) + '?c=' + e64(checksum)), {
          method: 'POST',
          body: chunkBuffer,
          headers: {
            'content-length': chunkBuffer.length
          }
        /* ... */
qgustavor commented 2 years ago

Oh, forget all the above! For some reason I missed the "without encryption" part of the entire question!

I noticed it too: https://github.com/qgustavor/mega/discussions/108 I will close this issue in favor of that discussion.