Search in sources :

Example 1 with RequestFile

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;
}
Also used : ArrayList(java.util.ArrayList) RequestFile(forpdateam.ru.forpda.api.RequestFile) AttachmentItem(forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem)

Example 2 with RequestFile

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;
}
Also used : JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) NetworkRequest(forpdateam.ru.forpda.api.NetworkRequest) RequestFile(forpdateam.ru.forpda.api.RequestFile) AttachmentItem(forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem)

Example 3 with RequestFile

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;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RequestFile(forpdateam.ru.forpda.api.RequestFile) File(java.io.File) RequestFile(forpdateam.ru.forpda.api.RequestFile) FileInputStream(java.io.FileInputStream)

Example 4 with 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;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) NetworkRequest(forpdateam.ru.forpda.api.NetworkRequest) RequestFile(forpdateam.ru.forpda.api.RequestFile) AttachmentItem(forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem)

Example 5 with RequestFile

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;
}
Also used : Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NetworkRequest(forpdateam.ru.forpda.api.NetworkRequest) AttachmentItem(forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem) ByteArrayInputStream(java.io.ByteArrayInputStream) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) RequestFile(forpdateam.ru.forpda.api.RequestFile) MessageDigest(java.security.MessageDigest)

Aggregations

RequestFile (forpdateam.ru.forpda.api.RequestFile)7 AttachmentItem (forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem)4 NetworkRequest (forpdateam.ru.forpda.api.NetworkRequest)3 NetworkResponse (forpdateam.ru.forpda.api.NetworkResponse)3 ArrayList (java.util.ArrayList)3 Matcher (java.util.regex.Matcher)3 InputStream (java.io.InputStream)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 MessageDigest (java.security.MessageDigest)1 HashMap (java.util.HashMap)1 JSONObject (org.json.JSONObject)1