use of forpdateam.ru.forpda.api.RequestFile in project ForPDA by RadiationX.
the class AttachmentsPopup method preUploadFiles.
public List<AttachmentItem> preUploadFiles(List<RequestFile> files) {
Log.d(LOG_TAG, "preUploadFiles " + files);
List<AttachmentItem> loadingItems = new ArrayList<>();
for (RequestFile file : files) {
AttachmentItem item = new AttachmentItem(file.getFileName());
item.setProgressListener(percent -> {
});
Log.d(LOG_TAG, "Add loading item " + item);
adapter.add(item);
loadingItems.add(item);
}
return loadingItems;
}
use of forpdateam.ru.forpda.api.RequestFile in project ForPDA by RadiationX.
the class Qms method uploadFiles.
public List<AttachmentItem> uploadFiles(List<RequestFile> files, List<AttachmentItem> pending) throws Exception {
String baseUrl = "https://ru.imgbb.com/";
String uploadUrl = "https://ru.imgbb.com/json";
String authToken = "null";
NetworkResponse baseResponse = Api.getWebClient().get(baseUrl);
Matcher baseMatcher = imgBbPattern.matcher(baseResponse.getBody());
if (baseMatcher.find()) {
uploadUrl = baseMatcher.group(1);
authToken = baseMatcher.group(2);
}
HashMap<String, String> headers = new HashMap<>();
headers.put("type", "file");
headers.put("action", "upload");
headers.put("privacy", "undefined");
headers.put("timestamp", Long.toString(System.currentTimeMillis()));
headers.put("auth_token", authToken);
headers.put("nsfw", "0");
// Matcher matcher = null;
for (int i = 0; i < files.size(); i++) {
RequestFile file = files.get(i);
AttachmentItem item = pending.get(i);
file.setRequestName("source");
NetworkRequest.Builder builder = new NetworkRequest.Builder().url(uploadUrl).formHeaders(headers).file(file);
NetworkResponse response = Api.getWebClient().request(builder.build(), item.getItemProgressListener());
JSONObject responseJson = new JSONObject(response.getBody());
forpdateam.ru.forpda.common.Utils.longLog(responseJson.toString(4));
if (responseJson.getInt("status_code") == 200) {
JSONObject imageJson = responseJson.getJSONObject("image");
item.setName(imageJson.getString("filename"));
item.setId(0);
item.setExtension(imageJson.getString("extension"));
item.setWeight(imageJson.getString("size_formatted"));
item.setTypeFile(AttachmentItem.TYPE_IMAGE);
item.setLoadState(AttachmentItem.STATE_LOADED);
item.setImageUrl(imageJson.getJSONObject("medium").getString("url"));
item.setUrl(imageJson.getJSONObject("image").getString("url"));
}
}
return pending;
}
use of forpdateam.ru.forpda.api.RequestFile in project ForPDA by RadiationX.
the class FilePickHelper method createFile.
private static RequestFile createFile(Context context, Uri uri) {
RequestFile requestFile = null;
Log.d(LOG_TAG, "createFile " + uri);
try {
InputStream inputStream = null;
String name = getFileName(context, uri);
String extension = MimeTypeUtil.getExtension(name);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
if (mimeType == null) {
mimeType = context.getContentResolver().getType(uri);
}
if (mimeType == null) {
mimeType = MimeTypeUtil.getType(extension);
}
if (uri.getScheme().equals("content")) {
inputStream = context.getContentResolver().openInputStream(uri);
} else if (uri.getScheme().equals("file")) {
inputStream = new FileInputStream(new File(uri.getPath()));
}
requestFile = new RequestFile(name, mimeType, inputStream);
} catch (Exception e) {
ACRA.getErrorReporter().handleException(e);
}
return requestFile;
}
use of forpdateam.ru.forpda.api.RequestFile in project ForPDA by RadiationX.
the class EditPost method uploadFiles.
public List<AttachmentItem> uploadFiles(int postId, List<RequestFile> files) throws Exception {
String url = "https://4pda.ru/forum/index.php?&act=attach&code=attach_upload_process&attach_rel_id=".concat(postId == 0 ? "" : Integer.toString(postId));
List<AttachmentItem> items = new ArrayList<>();
AttachmentItem item;
NetworkResponse response;
Matcher matcher;
for (RequestFile file : files) {
file.setRequestName("FILE_UPLOAD[]");
item = new AttachmentItem();
NetworkRequest.Builder builder = new NetworkRequest.Builder().url(url).file(file);
response = Api.getWebClient().request(builder.build());
matcher = loadedAttachments.matcher(response.getBody());
if (matcher.find())
item = fillAttachment(item, matcher);
matcher = statusInfo.matcher(response.getBody());
if (matcher.find())
fillAttachmentStatus(item, matcher);
items.add(item);
}
return items;
}
use of forpdateam.ru.forpda.api.RequestFile in project ForPDA by RadiationX.
the class EditPost method uploadFilesV2.
public List<AttachmentItem> uploadFilesV2(int postId, List<RequestFile> files, List<AttachmentItem> pending) throws Exception {
NetworkRequest.Builder builder = new NetworkRequest.Builder().url("https://4pda.ru/forum/index.php?act=attach").xhrHeader().formHeader("index", "1").formHeader("relId", Integer.toString(postId)).formHeader("maxSize", "134217728").formHeader("allowExt", "").formHeader("forum-attach-files", "").formHeader("code", "check");
NetworkResponse response;
Matcher matcher = null;
for (int i = 0; i < files.size(); i++) {
RequestFile file = files.get(i);
AttachmentItem item = pending.get(i);
file.setRequestName("FILE_UPLOAD[]");
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] targetArray = new byte[file.getFileStream().available()];
file.getFileStream().read(targetArray);
InputStream is1 = new ByteArrayInputStream(targetArray);
file.getFileStream().close();
file.setFileStream(is1);
messageDigest.update(targetArray);
byte[] hash = messageDigest.digest();
String md5 = ByteArraytoHexString(hash);
builder.formHeader("md5", md5).formHeader("size", "" + file.getFileStream().available()).formHeader("name", file.getFileName());
response = Api.getWebClient().request(builder.build());
if (response.getBody().equals("0")) {
NetworkRequest.Builder uploadRequest = new NetworkRequest.Builder().url("https://4pda.ru/forum/index.php?act=attach").xhrHeader().formHeader("index", "1").formHeader("relId", Integer.toString(postId)).formHeader("maxSize", "134217728").formHeader("allowExt", "").formHeader("forum-attach-files", "").formHeader("code", "upload").file(file);
response = Api.getWebClient().request(uploadRequest.build(), item.getProgressListener());
}
if (matcher == null) {
matcher = attachmentsPattern.matcher(response.getBody());
} else {
matcher = matcher.reset(response.getBody());
}
if (matcher.find()) {
fillAttachmentV2(item, matcher);
}
item.setStatus(AttachmentItem.STATUS_UPLOADED);
}
return pending;
}
Aggregations