Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload file starting from a specific byte #2339

Open
tatarinova0903 opened this issue Mar 1, 2024 · 0 comments
Open

Upload file starting from a specific byte #2339

tatarinova0903 opened this issue Mar 1, 2024 · 0 comments

Comments

@tatarinova0903
Copy link

tatarinova0903 commented Mar 1, 2024

Hi! I am using 15.0.0 version of Moya.

I need to upload file to server and listen to uploading progress. I've written this code:

struct StorageUploaderAPI: SignedTargetType {
    let baseURL: URL
    let fileURL: URL
    let blobLength: Int // file length
    let uploadedSize: Int // I should start uploading file from this byte
    let method = Moya.Method.put
    let path = ""

    init(baseURL: URL, fileURL: URL, blobLength: Int, uploadedSize: Int) {
        self.baseURL = baseURL
        self.fileURL = fileURL
        self.blobLength = blobLength
        self.uploadedSize = uploadedSize
    }

    var task: Task {
        .uploadFile(fileURL)
    }

    var apiHeaders: [String: String] {
       ...
    }
}

But it doesn't`t work if I should start uploading file from for example 100th byte.

So I changed creating task to this:

var task: Task {
        var data = try? Data(contentsOf: fileURL)
        if uploadedSize > 0 {
            data = data?.subdata(in: uploadedSize..<blobLength)
        }
        return .requestData(data ?? Data())
 }

But .requestData doesn't`t allow me to listen to progress of the request.

Which case of Task should I use in order to be able to upload file from a specific byte and to listen to uploading progress?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant