This commit is contained in:
kodjomoustapha 2024-01-24 18:37:21 +01:00
parent b88e782c06
commit c05b5927bb
11 changed files with 39 additions and 19 deletions

View file

@ -21,8 +21,8 @@ class MainActivity: FlutterActivity() {
).setMethodCallHandler { call, result ->
when (call.method) {
"start" -> {
val path = call.argument<String>("path")
Libmtorrentserver.start(path)
val config = call.argument<String>("config")
Libmtorrentserver.start(config)
result.success("ok")
}
else -> {

View file

@ -1,14 +1,17 @@
package main
import (
"encoding/json"
"server"
)
import "C"
//export Start
func Start(path *C.char) {
server.Start(C.GoString(path))
func Start(mcfg *C.char) {
var config server.Config
json.Unmarshal([]byte(C.GoString(mcfg)), &config)
server.Start(&config)
}
func main() {}

View file

@ -3,10 +3,14 @@ package libmtorrentserver
// #cgo LDFLAGS: -static-libstdc++
import "C"
import (
"encoding/json"
"server"
)
//export Start
func Start(path string) {
server.Start(path)
func Start(mcfg string) {
var config server.Config
json.Unmarshal([]byte(mcfg), &config)
server.Start(&config)
}

View file

@ -23,10 +23,11 @@ import (
var torrentCli *torrent.Client
var torrentcliCfg *torrent.ClientConfig
func Start(path string) {
func Start(config *Config) {
torrentcliCfg = torrent.NewDefaultClientConfig()
torrentcliCfg.DataDir = filepath.Clean(path)
torrentcliCfg.DataDir = filepath.Clean(config.Path)
log.Printf("[INFO] Download directory is set to: %s\n", torrentcliCfg.DataDir)
@ -69,8 +70,8 @@ func Start(path string) {
AllowCredentials: true,
})
log.Printf("[INFO] Listening on 127.0.0.1:8090")
log.Fatalln(http.ListenAndServe("127.0.0.1:8090", c.Handler(mux)))
log.Printf("[INFO] Listening on %s\n", config.Address)
log.Fatalln(http.ListenAndServe(config.Address, c.Handler(mux)))
}
@ -511,3 +512,8 @@ type torrentStatsFilesOnDisk struct {
BytesDownloaded int
FileSizeBytes int
}
type Config struct {
Address string `json:"address"`
Path string `json:"path"`
}

View file

@ -864,10 +864,10 @@ class TorrentLibrary {
__FCmulcrPtr.asFunction<_Fcomplex Function(_Fcomplex, double)>();
void Start(
ffi.Pointer<ffi.Char> path,
ffi.Pointer<ffi.Char> mcfg,
) {
return _Start(
path,
mcfg,
);
}

View file

@ -74,7 +74,7 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
extern "C" {
#endif
extern __declspec(dllexport) void Start(char* path);
extern __declspec(dllexport) void Start(char* mcfg);
#ifdef __cplusplus
}

View file

@ -10,7 +10,7 @@ import 'generated_bindings.dart';
/// For very short-lived functions, it is fine to call them on the main isolate.
/// They will block the Dart execution while running the native function, so
/// only do this for native functions which are guaranteed to be short-lived.
void start(String path) => _bindings.Start(path.toNativeUtf8().cast());
void start(String mcfg) => _bindings.Start(mcfg.toNativeUtf8().cast());
const String _libName = 'libmtorrentserver';

View file

@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
import 'package:flutter/services.dart';
@ -10,7 +11,8 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'torrent_server.g.dart';
class MTorrentServer {
final _baseUrl = "http://127.0.0.1:8090";
final _baseUrl =
Platform.isLinux ? "http://127.0.0.1:8090" : "http://127.0.0.1:3535";
Future<bool> removeTorrent(String? inforHash) async {
if (inforHash == null || inforHash.isEmpty) return false;
@ -54,19 +56,24 @@ class MTorrentServer {
}
Future<(List<Video>, String?)> getTorrentPlaylist(String url) async {
final path = (await StorageProvider().getBtDirectory())!.path;
final isRunning = await check();
if (!isRunning) {
final address =
_baseUrl.replaceAll("http://", "").replaceAll("https://", "");
final path = (await StorageProvider().getBtDirectory())!.path;
final config = jsonEncode({"path": path, "address": address});
if (Platform.isAndroid) {
const channel =
MethodChannel('com.kodjodevf.mangayomi.libmtorrentserver');
channel.invokeMethod('start', {"path": path});
channel.invokeMethod('start', {"config": config});
} else {
await Isolate.run(() async {
libmtorrentserver_ffi.start(path);
libmtorrentserver_ffi.start(config);
});
}
}
bool isMagnet = !url.startsWith("http");
String finalUrl = "";
String? infohash;

Binary file not shown.

View file

@ -74,7 +74,7 @@ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
extern "C" {
#endif
extern __declspec(dllexport) void Start(char* path);
extern __declspec(dllexport) void Start(char* mcfg);
#ifdef __cplusplus
}