Search in sources :

Example 11 with AttachmentItem

use of forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem 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)

Example 12 with AttachmentItem

use of forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem in project ForPDA by RadiationX.

the class EditPost method loadForm.

public EditPostForm loadForm(int postId) throws Exception {
    EditPostForm form = new EditPostForm();
    String url = "https://4pda.ru/forum/index.php?act=post&do=edit_post&p=".concat(Integer.toString(postId));
    // url = url.concat("&t=").concat(Integer.toString(topicId)).concat("&f=").concat(Integer.toString(forumId));
    NetworkResponse response = Api.getWebClient().get(url);
    if (response.getBody().equals("nopermission")) {
        form.setErrorCode(EditPostForm.ERROR_NO_PERMISSION);
        return form;
    }
    Matcher matcher = postPattern.matcher(response.getBody());
    if (matcher.find()) {
        form.setMessage(ApiUtils.fromHtml(ApiUtils.escapeNewLine(matcher.group(1))));
        form.setEditReason(matcher.group(2));
    }
    matcher = pollInfoPattern.matcher(response.getBody());
    if (matcher.find()) {
        EditPoll poll = createPoll(matcher);
        form.setPoll(poll);
        EditPost.printPoll(poll);
    }
    response = Api.getWebClient().get("https://4pda.ru/forum/index.php?act=attach&index=1&relId=" + postId + "&maxSize=134217728&allowExt=&code=init&unlinked=");
    matcher = attachmentsPattern.matcher(response.getBody());
    while (matcher.find()) {
        form.addAttachment(fillAttachmentV2(new AttachmentItem(), matcher));
    }
    return form;
}
Also used : EditPostForm(forpdateam.ru.forpda.api.theme.editpost.models.EditPostForm) Matcher(java.util.regex.Matcher) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) EditPoll(forpdateam.ru.forpda.api.theme.editpost.models.EditPoll) AttachmentItem(forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem)

Example 13 with AttachmentItem

use of forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem in project ForPDA by RadiationX.

the class EditPostFragment method sendMessage.

private void sendMessage() {
    messagePanel.setProgressState(true);
    postForm.setMessage(messagePanel.getMessage());
    List<AttachmentItem> attachments = messagePanel.getAttachments();
    postForm.getAttachments().clear();
    for (AttachmentItem item : attachments) {
        postForm.addAttachment(item);
    }
    subscribe(RxApi.EditPost().sendPost(postForm), s -> {
        messagePanel.setProgressState(false);
        if (s.getId() != 0) {
            TabFragment fragment = TabManager.get().get(getParentTag());
            if (fragment != null) {
                if (fragment instanceof ThemeFragment) {
                    ThemeFragment themeFragment = (ThemeFragment) fragment;
                    if (postForm.getType() == TYPE_EDIT_POST) {
                        themeFragment.onEditPostCompleted(s);
                    } else {
                        themeFragment.onSendPostCompleted(s);
                    }
                }
            }
            TabManager.get().remove(EditPostFragment.this);
        }
    }, new ThemePage(), v -> loadData());
}
Also used : ThemePage(forpdateam.ru.forpda.api.theme.models.ThemePage) TabFragment(forpdateam.ru.forpda.ui.fragments.TabFragment) AttachmentItem(forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem) ThemeFragment(forpdateam.ru.forpda.ui.fragments.theme.ThemeFragment)

Example 14 with AttachmentItem

use of forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem in project ForPDA by RadiationX.

the class AttachmentAdapter method deleteSelected.

public void deleteSelected() {
    for (AttachmentItem item : selected) {
        if (item.getStatus() == AttachmentItem.STATUS_REMOVED) {
            int index = items.indexOf(item);
            items.remove(index);
            notifyItemRemoved(index);
        }
    }
    unSelectItems();
    if (onDataChangeListener != null) {
        onDataChangeListener.onChange(items.size());
    }
}
Also used : AttachmentItem(forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem)

Example 15 with AttachmentItem

use of forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem in project ForPDA by RadiationX.

the class AttachmentsPopup method insertAttachment.

public void insertAttachment(List<AttachmentItem> items, boolean toSpoiler) {
    StringBuilder text = new StringBuilder();
    if (toSpoiler)
        text.append("[spoiler]");
    for (AttachmentItem item : items) {
        if (insertAttachmentListener != null) {
            text.append(insertAttachmentListener.onInsert(item));
        } else {
            text.append("[attachment=").append(item.getId()).append(":").append(item.getName()).append("]");
        }
    }
    if (toSpoiler)
        text.append("[/spoiler]");
    messagePanel.insertText(text.toString());
    adapter.unSelectItems();
    dialog.cancel();
}
Also used : AttachmentItem(forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem)

Aggregations

AttachmentItem (forpdateam.ru.forpda.api.theme.editpost.models.AttachmentItem)16 NetworkResponse (forpdateam.ru.forpda.api.NetworkResponse)6 Matcher (java.util.regex.Matcher)6 NetworkRequest (forpdateam.ru.forpda.api.NetworkRequest)4 RequestFile (forpdateam.ru.forpda.api.RequestFile)4 ArrayList (java.util.ArrayList)3 EditPostForm (forpdateam.ru.forpda.api.theme.editpost.models.EditPostForm)2 EditPoll (forpdateam.ru.forpda.api.theme.editpost.models.EditPoll)1 ThemePage (forpdateam.ru.forpda.api.theme.models.ThemePage)1 CustomWebChromeClient (forpdateam.ru.forpda.common.webview.CustomWebChromeClient)1 CustomWebViewClient (forpdateam.ru.forpda.common.webview.CustomWebViewClient)1 TabFragment (forpdateam.ru.forpda.ui.fragments.TabFragment)1 ThemeFragment (forpdateam.ru.forpda.ui.fragments.theme.ThemeFragment)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 MessageDigest (java.security.MessageDigest)1 HashMap (java.util.HashMap)1 JSONObject (org.json.JSONObject)1