-
Notifications
You must be signed in to change notification settings - Fork 5
Feat/download file #30
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f4739aa
feat: add FileDownloader class for file download functionality and re…
patrickkabwe 2823e3d
docs: update README to include additional information
patrickkabwe a84b69a
refactor: remove Alamofire dependency and update PrivacyInfo.xcprivac…
patrickkabwe 3938edd
refactor: remove unused methods from NitroFSFileUploader class
patrickkabwe cb27c21
Update NitroFSImpl.kt
patrickkabwe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ on: | |
| push: | ||
| branches: | ||
| - main | ||
| - next | ||
| paths-ignore: | ||
| - 'docs/**' | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.nitrofs | ||
|
|
||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import io.ktor.client.HttpClient | ||
| import io.ktor.client.call.body | ||
| import io.ktor.client.engine.okhttp.OkHttp | ||
| import io.ktor.client.plugins.onDownload | ||
| import io.ktor.client.request.prepareGet | ||
| import io.ktor.http.HttpMethod | ||
| import io.ktor.util.cio.writeChannel | ||
| import io.ktor.utils.io.ByteReadChannel | ||
| import io.ktor.utils.io.copyAndClose | ||
| import java.io.File | ||
|
|
||
| class FileDownloader { | ||
| suspend fun downloadFile( | ||
| serverUrl: String, | ||
| fileName: String, | ||
| destinationPath: String, | ||
| onProgress: ((Double, Double) -> Unit)? | ||
| ) { | ||
| val outputFile = File(destinationPath) | ||
| outputFile.parentFile?.mkdirs() | ||
| val client = HttpClient(OkHttp) | ||
|
|
||
| client.use { it | ||
| it.prepareGet("$serverUrl/$fileName") { | ||
| method = HttpMethod.Get | ||
| onDownload { bytesSentTotal, contentLength -> | ||
| if (bytesSentTotal > 0){ | ||
| onProgress?.let { | ||
| Handler(Looper.getMainLooper()).post { | ||
| onProgress.invoke(bytesSentTotal.toDouble(), contentLength.toDouble()) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }.execute { response -> | ||
| val channel: ByteReadChannel = response.body() | ||
| channel.copyAndClose(outputFile.writeChannel()) | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Well-implemented file download functionality.
The new
FileDownloaderclass is well-structured with several good practices:useblock (lines 25-43)copyAndCloseto write directly to file (line 41)However, there are a few opportunities for improvement:
Consider adding error handling for HTTP responses and timeouts:
val client = HttpClient(OkHttp) { + engine { + config { + connectTimeout(30, TimeUnit.SECONDS) + readTimeout(30, TimeUnit.SECONDS) + } + } } client.use { it it.prepareGet("$serverUrl/$fileName") { method = HttpMethod.Get onDownload { bytesSentTotal, contentLength -> // ... existing code ... } }.execute { response -> + if (!response.status.isSuccess()) { + throw IOException("Download failed with status: ${response.status}") + } val channel: ByteReadChannel = response.body() channel.copyAndClose(outputFile.writeChannel()) } }📝 Committable suggestion
🤖 Prompt for AI Agents