ale46 / mega-java Goto Github PK
View Code? Open in Web Editor NEWJava library for the mega.co.nz API
Java library for the mega.co.nz API
When downloading files from Mega
, sometimes exceptions like the following occur:
java.io.IOException: Server returned HTTP response code: 500 for URL: https://g.api.mega.co.nz/cs?id=886954536
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at application.downloader.mega.MegaHandler.api_request(MegaHandler.java:69)
at application.downloader.mega.MegaHandler.download(MegaHandler.java:138)
at application.downloader.mega.MegaHandler.download(MegaHandler.java:107)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
at org.json.JSONObject.<init>(JSONObject.java:195)
at org.json.JSONObject.<init>(JSONObject.java:319)
at application.downloader.mega.MegaHandler.download(MegaHandler.java:138)
at application.downloader.mega.MegaHandler.download(MegaHandler.java:107)
This happens for the same file so it's an unreliable download. If possible, please fix this.
Note that the line numbers given are wrong since I slightly edited the code myself as well but that is not the reason for the instability.
Hi,
I have used your api to download files from mega, but I don't want to log in because I only want to download things from mega in java.
How can I realize this?
java.lang.NoClassDefFoundError: org/json/JSONException
Servlet.MegaServlet.doGet(MegaServlet.java:19)
javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:46)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$36(SecurityContextThreadSetupAction.java:105)
org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$40(UndertowDeploymentInfoService.java:1706)
org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$40(UndertowDeploymentInfoService.java:1706)
org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$40(UndertowDeploymentInfoService.java:1706)
org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$40(UndertowDeploymentInfoService.java:1706)
org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$40(UndertowDeploymentInfoService.java:1706)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
io.undertow.server.Connectors.executeRootHandler(Connectors.java:211)
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:809)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)
The base64_url_decode_byte()
method does not behave identical on Windows and Linux. On Windows, the amount of separator characters (variable sepCnt
) is 0 while for Linux it is 1 for the same input.
Forcing the sepCnt
to 0
causes a further exception:
java.lang.StringIndexOutOfBoundsException: String index out of range: 44
I know that this repository is deprecated but I just wanted to let you know about this inconsistency. Maybe you know a fix.
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 =)
Nope, File name is now something else.
What I got is: eA9f-fMYfFaNpVbchl44bwRcbfBU
I will stick to the filecreating and split method for now, and once you fixed It I will do a pull request about an idea.
Two methods are missing try catches or throws Exception. I also showed that in the pull request.
Can you add the upload support?
where did you get documentation about the fields a json request should contain?
as instance, here
public String add_user(String email) {
JSONObject json = new JSONObject();
try {
json.put("a", "ur");
json.put("u", email);
json.put("l", 1);
} catch (JSONException e) {
e.printStackTrace();
}
return api_request(json.toString());
}
how do you know those "a","u","l" ? Is there a website or something?
also, I do not need the whole functions from the official sdk; do you think is still possible to send POST and GET requests to them?
If you list all files and create the URL from a file, the key in it is wrong (and too short).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.