// protection and token
HashMap info = getPageInfo("File:" + filename);
if (!checkRights(info, "upload"))
{
CredentialException ex = new CredentialException("Permission denied: page is protected.");
log(Level.WARNING, "upload", "Cannot upload - permission denied." + ex);
throw ex;
}
String wpEditToken = (String)info.get("token");
// chunked upload setup
long filesize = file.length();
long chunks = (filesize >> LOG2_CHUNK_SIZE) + 1;
FileInputStream fi = new FileInputStream(file);
String filekey = "";
// upload the image
for (int i = 0; i < chunks; i++)
{
HashMap<String, Object> params = new HashMap<String, Object>(50);
params.put("filename", filename);
params.put("token", wpEditToken);
params.put("ignorewarnings", "true");
if (chunks == 1)
{
// Chunks disabled due to a small filesize.
// This is just a normal upload.
params.put("text", contents);
if (!reason.isEmpty())
params.put("comment", reason);
byte[] by = new byte[fi.available()];
fi.read(by);
// Why this is necessary?
params.put("file\"; filename=\"" + file.getName(), by);
}
else
{
long offset = i << LOG2_CHUNK_SIZE;
params.put("stash", "1");
params.put("offset", "" + offset);
params.put("filesize", "" + filesize);
if (i != 0)
params.put("filekey", filekey);
// write the actual file
long buffersize = Math.min(1 << LOG2_CHUNK_SIZE, filesize - offset);
byte[] by = new byte[(int)buffersize]; // 32 bit problem. Why must array indices be ints?
fi.read(by);
params.put("chunk\"; filename=\"" + file.getName(), by);
// Each chunk presumably requires a new edit token
wpEditToken = (String)getPageInfo("File:" + filename).get("token");
}
// done
String response = multipartPost(apiUrl + "action=upload", params, "upload");
try
{
// look for filekey
if (chunks > 1)
{
if (response.contains("filekey=\""))
{
filekey = parseAttribute(response, "filekey", 0);
continue;
}
else
throw new IOException("No filekey present! Server response was " + response);
}
// TODO: check for more specific errors here
if (response.contains("error code=\"fileexists-shared-forbidden\""))
{
CredentialException ex = new CredentialException("Cannot overwrite file hosted on central repository.");
log(Level.WARNING, "upload", "Cannot upload - permission denied." + ex);
throw ex;
}
checkErrorsAndUpdateStatus(response, "upload");
}