Skip to content

Commit

Permalink
feat: rest api to add silent downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Talha Ahmed committed Oct 8, 2024
1 parent 34801da commit cf1e9b9
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 22 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ on:
tags:
- "v[0-9]+.[0-9]+.[0-9]+*"


permissions:
contents: write

jobs:
create-packages:
strategy:
matrix:
os: [ "ubuntu-latest" , "windows-latest" ]
os: ["ubuntu-latest", "windows-latest"]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
Expand Down Expand Up @@ -43,9 +42,9 @@ jobs:
chmod 755 ./gradlew
shell: "bash"
- name: Gradle
# at the current version of compose plugin
# first attempt to init gradle always fails because of "BuildScopeServices has been closed"
# remove this after this bug is fixed
# at the current version of compose plugin
# first attempt to init gradle always fails because of "BuildScopeServices has been closed"
# remove this after this bug is fixed
continue-on-error: true
run: |
./gradlew
Expand Down Expand Up @@ -97,4 +96,4 @@ jobs:
- name: "Remove artifacts to free space"
uses: geekyeggo/delete-artifact@v5
with:
name: app-*
name: app-*
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ follow these steps.

1. Clone the project
2. Install the [JBR](https://github.com/JetBrains/JetBrainsRuntime/releases)
1. If you extract it to your home directory and don't want to modify global `JAVA_HOME` environment variable you can set `JAVA_HOME` to the extracted directory e.g. `export JAVA_HOME=~/jbrsdk_jcef-21.0.4-linux-x64-b620.4` for linux or `$env:JAVA_HOME="C:\path\to\jbr"` on Windows
3. cd into the project, open your terminal and execute the following commands
4. select which way you want to compile the app
<details>
Expand Down
42 changes: 42 additions & 0 deletions REST-API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
To lazy to do it in openapi v3.
Default port is `15151`

## Endpoints

- `POST /add`
- ```ts
interface Request{
body: {
link: string;
headers?: Record<string,string>
downloadPage?: string
}
}
```
- `GET /queues`
- Returns list of queues
- ```ts
interface Queue{
id: number
name: string
}
type Response = {
body: Queue[]
}
```
- `POST /add-download-task`
- ```ts
interface Request{
body: {
downloadSource: {
link: string;
headers?: Record<string,string>;
downloadPage?: string;
}
folder?: string; //unix style path separator
name?: string;
folder?: string;
queueId?: number;
}
}
```
Original file line number Diff line number Diff line change
@@ -1,21 +1,62 @@
package com.abdownloadmanager.desktop.integration

import com.abdownloadmanager.integration.IntegrationHandler
import com.abdownloadmanager.desktop.AppComponent
import ir.amirab.downloader.downloaditem.DownloadCredentials
import com.abdownloadmanager.desktop.repository.AppRepository
import com.abdownloadmanager.desktop.utils.DownloadSystem
import com.abdownloadmanager.integration.IntegrationHandler
import com.abdownloadmanager.integration.NewDownloadInfoFromIntegration
import com.abdownloadmanager.integration.NewDownloadTask
import com.abdownloadmanager.integration.QueueModel
import ir.amirab.downloader.downloaditem.DownloadCredentials
import ir.amirab.downloader.downloaditem.DownloadItem
import ir.amirab.downloader.queue.QueueManager
import ir.amirab.downloader.utils.OnDuplicateStrategy
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class IntegrationHandlerImp: IntegrationHandler,KoinComponent{
class IntegrationHandlerImp : IntegrationHandler, KoinComponent {
val appComponent by inject<AppComponent>()
val downloadSystem by inject<DownloadSystem>()
val queueManager by inject<QueueManager>()
val appSettings by inject<AppRepository>()
override suspend fun addDownload(list: List<NewDownloadInfoFromIntegration>) {
appComponent.openAddDownloadDialog(list.map {
DownloadCredentials(
link = it.link,
headers = it.headers,
downloadPage = it.downloadPage,
)
})
appComponent.openAddDownloadDialog(
list.map {
DownloadCredentials(
link = it.link,
headers = it.headers,
downloadPage = it.downloadPage,
)
}
)
}
override fun listQueues(): List<QueueModel> {
return queueManager.getAll().map { downloadQueue ->
val queueModel = downloadQueue.getQueueModel()
QueueModel(id = queueModel.id, name = queueModel.name)
}
}
override suspend fun addDownloadTask(task: NewDownloadTask) {
val downloadItem =
DownloadItem(
link = task.downloadSource.link,
headers = task.downloadSource.headers,
downloadPage = task.downloadSource.downloadPage,
folder = task.folder ?: appSettings.saveLocation.value,
id = -1,
name = task.name ?: task.downloadSource.link.substringAfterLast("/"),
)
val id =
downloadSystem.addDownload(
downloadItem,
OnDuplicateStrategy.default(),
task.queueId
)
if (task.queueId != null) {
val queue = queueManager.getQueue(task.queueId!!)
queue.start()
} else {
downloadSystem.manualResume(id)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.abdownloadmanager.integration.http4k.MyHttp4KServer
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.builtins.ListSerializer

//val scope = CoroutineScope(SupervisorJob())

Expand Down Expand Up @@ -114,6 +115,24 @@ class Integration(
}
MyResponse.Text("OK")
}
get("/queues") {
runBlocking {
val queues = integrationHandler.listQueues()
val jsonResponse = customJson.encodeToString(ListSerializer(QueueModel.serializer()), queues)
MyResponse.Text(jsonResponse)
}
}
post("/add-download-task") {
runBlocking {
val itemsToAdd = kotlin.runCatching {
val message = it.getBody().orEmpty()
customJson.decodeFromString<NewDownloadTask>(message)
}
itemsToAdd.onFailure { it.printStackTrace() }
integrationHandler.addDownloadTask(itemsToAdd.getOrThrow())
}
MyResponse.Text("OK")
}
post("/ping") {
MyResponse.Text("pong")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.abdownloadmanager.integration

interface IntegrationHandler{
suspend fun addDownload(
list: List<NewDownloadInfoFromIntegration>
)
}
interface IntegrationHandler {
suspend fun addDownload(list: List<NewDownloadInfoFromIntegration>)
fun listQueues(): List<QueueModel>
suspend fun addDownloadTask(task: NewDownloadTask)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.abdownloadmanager.integration

import kotlinx.serialization.Serializable

@Serializable
data class NewDownloadTask(
val downloadSource: NewDownloadInfoFromIntegration,
var folder: String? = null,
var name: String? = null,
var queueId: Long? = null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.abdownloadmanager.integration

import kotlinx.serialization.Serializable

@Serializable
data class QueueModel(
val id: Long,
val name: String,
)

0 comments on commit cf1e9b9

Please sign in to comment.