Skip to content

Resume download #1

Description

@trodox

Hello, I see that every time you create the script the file it was destroy and create another one (when you download them), I try to modify some of your code but isn't work.. I use HTTPClient

public void download(String url, String path) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IOException, IllegalBlockSizeException, BadPaddingException, JSONException{
    //TODO DOWNLOAD mismatch?
    print("Download started");
    String[] s = url.split("!");
    String file_id = s[1];
    byte[] file_key = MegaCrypt.base64_url_decode_byte(s[2]); 

    int[] intKey = MegaCrypt.aByte_to_aInt(file_key);
    JSONObject json = new JSONObject();
    try {
        json.put("a", "g");
        json.put("g", "1");
        json.put("p", file_id);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONObject file_data = new JSONObject(api_request(json.toString()));
    int[] keyNOnce = new int[] { intKey[0] ^ intKey[4], intKey[1] ^ intKey[5], intKey[2] ^ intKey[6], intKey[3] ^ intKey[7], intKey[4], intKey[5] };
    byte[] key = MegaCrypt.aInt_to_aByte(keyNOnce[0], keyNOnce[1], keyNOnce[2], keyNOnce[3]);

    int[] iiv = new int[] { keyNOnce[4], keyNOnce[5], 0, 0 };
    byte[] iv = MegaCrypt.aInt_to_aByte(iiv);

    @SuppressWarnings("unused")
    int file_size = file_data.getInt("s");
    String attribs = (file_data.getString("at"));
    attribs = new String(MegaCrypt.aes_cbc_decrypt(MegaCrypt.base64_url_decode_byte(attribs), key));

    String file_name = attribs.substring(10,attribs.lastIndexOf("\""));
    print(file_name);   //Have the real filename
    file_name = URLEncoder.encode(file_name);
    File file = new File(file_name);

    int size = (int) file.length();


    final IvParameterSpec ivSpec = new IvParameterSpec(iv);
    final SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/CTR/nopadding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);

    String file_url = file_data.getString("g");

    FileOutputStream fos = new FileOutputStream(path+File.separator+file_name, true);
    cos = new CipherOutputStream(fos, cipher);

    Cipher decipher = Cipher.getInstance("AES/CTR/NoPadding"); 
    decipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);

    byte[] buffer;





    print("Last read byte " + size );

    try {


        print(file_url);

        DefaultHttpClient httpClient1 = new DefaultHttpClient();
        HttpGet httpGet1 = new HttpGet(file_url);
        httpGet1.addHeader("Range", "bytes=" + size + "-"); 
        httpGet1.addHeader("Modified", "bytes=" + size + "-");



        HttpResponse httpResponse = httpClient1.execute(httpGet1);

        HttpEntity httpEntity = httpResponse.getEntity();
        InputStream httpInputStream = httpEntity.getContent();

        print("File: " + file.getAbsolutePath());
        int downloaded = size;

      //Sets a new connection to get the real size of the download (because if we get the other one we get a wrong size (download - total) )
        DefaultHttpClient httpSize = new DefaultHttpClient();
        HttpGet httpGetSize = new HttpGet(file_url);
        HttpResponse httpResponseSize = httpSize.execute(httpGetSize);

        int total_size = Integer.parseInt(httpResponseSize.getFirstHeader("Content-Length").getValue());

        if (total_size == size){
            System.out.println("Already downloaded.");

        }else{

            while (true) {

                buffer = new byte[1024];

                int read = httpInputStream.read(buffer);     // Read from server into buffer.
                if (read == -1) //If we get -1 it's EOF.
                    break;

                cos.write(buffer, 0, read);     // Write buffer to file.    

                //cos.flush();

                downloaded += read;
                System.out.println("read bytes: "+downloaded +" (Kb: " + downloaded/1024+")");
            }
        }
    } finally {

            if (fos != null) {
                fos.close();
            }

    }
    print("Download finished");
}

Thanks =)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions