package net.gotev.uploadservice;

import android.content.Intent;
import com.bumptech.glide.load.Key;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import net.gotev.uploadservice.http.BodyWriter;
/* loaded from: classes.dex */
public class MultipartUploadTask extends HttpUploadTask {
    private static final String BOUNDARY_SIGNATURE = "-------AndroidUploadService";
    private static final String NEW_LINE = "\r\n";
    protected static final String PARAM_UTF8_CHARSET = "multipartUtf8Charset";
    protected static final String PROPERTY_CONTENT_TYPE = "httpContentType";
    protected static final String PROPERTY_PARAM_NAME = "httpParamName";
    protected static final String PROPERTY_REMOTE_FILE_NAME = "httpRemoteFileName";
    private static final String TWO_HYPHENS = "--";
    private static final Charset US_ASCII = Charset.forName("US-ASCII");
    private byte[] boundaryBytes;
    private Charset charset;
    private byte[] trailerBytes;

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // net.gotev.uploadservice.HttpUploadTask, net.gotev.uploadservice.UploadTask
    public void init(UploadService uploadService, Intent intent) throws IOException {
        super.init(uploadService, intent);
        String str = BOUNDARY_SIGNATURE + System.currentTimeMillis();
        Charset charset = US_ASCII;
        this.boundaryBytes = ("\r\n--" + str + NEW_LINE).getBytes(charset);
        this.trailerBytes = ("\r\n--" + str + TWO_HYPHENS + NEW_LINE).getBytes(charset);
        if (intent.getBooleanExtra(PARAM_UTF8_CHARSET, false)) {
            charset = Charset.forName(Key.STRING_CHARSET_NAME);
        }
        this.charset = charset;
        if (this.params.getFiles().size() <= 1) {
            this.httpParams.addRequestHeader("Connection", "close");
        } else {
            this.httpParams.addRequestHeader("Connection", "Keep-Alive");
        }
        this.httpParams.addRequestHeader("Content-Type", "multipart/form-data; boundary=" + str);
    }

    @Override // net.gotev.uploadservice.HttpUploadTask
    protected long getBodyLength() throws UnsupportedEncodingException {
        return getRequestParametersLength() + getFilesLength() + this.trailerBytes.length;
    }

    @Override // net.gotev.uploadservice.http.HttpConnection.RequestBodyDelegate
    public void onBodyReady(BodyWriter bodyWriter) throws IOException {
        writeRequestParameters(bodyWriter);
        writeFiles(bodyWriter);
        bodyWriter.write(this.trailerBytes);
    }

    private long getFilesLength() throws UnsupportedEncodingException {
        long j = 0;
        for (UploadFile uploadFile : this.params.getFiles()) {
            j += getTotalMultipartBytes(uploadFile);
        }
        return j;
    }

    private long getRequestParametersLength() throws UnsupportedEncodingException {
        long j = 0;
        if (!this.httpParams.getRequestParameters().isEmpty()) {
            for (NameValue nameValue : this.httpParams.getRequestParameters()) {
                j += this.boundaryBytes.length + getMultipartBytes(nameValue).length;
            }
        }
        return j;
    }

    private byte[] getMultipartBytes(NameValue nameValue) throws UnsupportedEncodingException {
        return ("Content-Disposition: form-data; name=\"" + nameValue.getName() + "\"" + NEW_LINE + NEW_LINE + nameValue.getValue()).getBytes(this.charset);
    }

    private byte[] getMultipartHeader(UploadFile uploadFile) throws UnsupportedEncodingException {
        return ("Content-Disposition: form-data; name=\"" + uploadFile.getProperty(PROPERTY_PARAM_NAME) + "\"; filename=\"" + uploadFile.getProperty(PROPERTY_REMOTE_FILE_NAME) + "\"" + NEW_LINE + "Content-Type: " + uploadFile.getProperty(PROPERTY_CONTENT_TYPE) + NEW_LINE + NEW_LINE).getBytes(this.charset);
    }

    private long getTotalMultipartBytes(UploadFile uploadFile) throws UnsupportedEncodingException {
        return this.boundaryBytes.length + getMultipartHeader(uploadFile).length + uploadFile.length(this.service);
    }

    private void writeRequestParameters(BodyWriter bodyWriter) throws IOException {
        byte[] multipartBytes;
        if (this.httpParams.getRequestParameters().isEmpty()) {
            return;
        }
        for (NameValue nameValue : this.httpParams.getRequestParameters()) {
            bodyWriter.write(this.boundaryBytes);
            bodyWriter.write(getMultipartBytes(nameValue));
            this.uploadedBytes += this.boundaryBytes.length + multipartBytes.length;
            broadcastProgress(this.uploadedBytes, this.totalBytes);
        }
    }

    private void writeFiles(BodyWriter bodyWriter) throws IOException {
        byte[] multipartHeader;
        for (UploadFile uploadFile : this.params.getFiles()) {
            if (!this.shouldContinue) {
                return;
            }
            bodyWriter.write(this.boundaryBytes);
            bodyWriter.write(getMultipartHeader(uploadFile));
            this.uploadedBytes += this.boundaryBytes.length + multipartHeader.length;
            broadcastProgress(this.uploadedBytes, this.totalBytes);
            InputStream stream = uploadFile.getStream(this.service);
            bodyWriter.writeStream(stream, this);
            stream.close();
        }
    }

    @Override // net.gotev.uploadservice.UploadTask
    protected void onSuccessfulUpload() {
        for (UploadFile uploadFile : this.params.getFiles()) {
            addSuccessfullyUploadedFile(uploadFile.getPath());
        }
        this.params.getFiles().clear();
    }
}
