mirror of
https://github.com/abraunegg/onedrive
synced 2024-06-04 23:12:18 +02:00
capability to run as service
This commit is contained in:
parent
3f40728779
commit
ea970890a2
4
Makefile
4
Makefile
|
@ -27,8 +27,10 @@ clean:
|
||||||
|
|
||||||
install: onedrive onedrive.conf
|
install: onedrive onedrive.conf
|
||||||
install onedrive $(DESTDIR)
|
install onedrive $(DESTDIR)
|
||||||
install onedrive.conf $(CONFDIR)
|
install -m 644 onedrive.conf $(CONFDIR)
|
||||||
|
install -m 644 onedrive.service /usr/lib/systemd/user
|
||||||
|
|
||||||
uninstall:
|
uninstall:
|
||||||
rm -f $(DESTDIR)/onedrive
|
rm -f $(DESTDIR)/onedrive
|
||||||
rm -f $(CONFDIR)/onedrive.conf
|
rm -f $(CONFDIR)/onedrive.conf
|
||||||
|
rm -f /usr/lib/systemd/user/onedrive.service
|
||||||
|
|
10
onedrive.service
Normal file
10
onedrive.service
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
[Unit]
|
||||||
|
Description=OneDrive Free Client
|
||||||
|
Documentation=https://github.com/skilion/onedrive
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=/usr/local/bin/onedrive -m
|
||||||
|
Restart=no
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=default.target
|
21
src/main.d
21
src/main.d
|
@ -1,6 +1,6 @@
|
||||||
import core.memory, core.time, core.thread;
|
import core.memory, core.time, core.thread;
|
||||||
import std.getopt, std.file, std.path, std.process, std.stdio;
|
import std.getopt, std.file, std.path, std.process, std.stdio;
|
||||||
import config, itemdb, monitor, onedrive, sync;
|
import config, itemdb, monitor, onedrive, sync, util;
|
||||||
|
|
||||||
|
|
||||||
void main(string[] args)
|
void main(string[] args)
|
||||||
|
@ -50,6 +50,11 @@ void main(string[] args)
|
||||||
auto cfg = config.Config(configFile1Path, configFile2Path, configFile3Path);
|
auto cfg = config.Config(configFile1Path, configFile2Path, configFile3Path);
|
||||||
|
|
||||||
if (verbose) writeln("Initializing the OneDrive API ...");
|
if (verbose) writeln("Initializing the OneDrive API ...");
|
||||||
|
bool online = testNetwork();
|
||||||
|
if (!online && !monitor) {
|
||||||
|
writeln("No network connection");
|
||||||
|
return;
|
||||||
|
}
|
||||||
auto onedrive = new OneDriveApi(cfg, verbose);
|
auto onedrive = new OneDriveApi(cfg, verbose);
|
||||||
onedrive.onRefreshToken = (string refreshToken) {
|
onedrive.onRefreshToken = (string refreshToken) {
|
||||||
std.file.write(refreshTokenFilePath, refreshToken);
|
std.file.write(refreshTokenFilePath, refreshToken);
|
||||||
|
@ -60,7 +65,6 @@ void main(string[] args)
|
||||||
} catch (FileException e) {
|
} catch (FileException e) {
|
||||||
onedrive.authorize();
|
onedrive.authorize();
|
||||||
}
|
}
|
||||||
// TODO check if the token is valid
|
|
||||||
|
|
||||||
if (verbose) writeln("Opening the item database ...");
|
if (verbose) writeln("Opening the item database ...");
|
||||||
auto itemdb = new ItemDatabase(databaseFilePath);
|
auto itemdb = new ItemDatabase(databaseFilePath);
|
||||||
|
@ -81,7 +85,7 @@ void main(string[] args)
|
||||||
// swallow exception
|
// swallow exception
|
||||||
}
|
}
|
||||||
sync.init(statusToken);
|
sync.init(statusToken);
|
||||||
performSync(sync);
|
if (online) performSync(sync);
|
||||||
|
|
||||||
if (monitor) {
|
if (monitor) {
|
||||||
if (verbose) writeln("Initializing monitor ...");
|
if (verbose) writeln("Initializing monitor ...");
|
||||||
|
@ -123,13 +127,16 @@ void main(string[] args)
|
||||||
immutable auto checkInterval = dur!"seconds"(45);
|
immutable auto checkInterval = dur!"seconds"(45);
|
||||||
auto lastCheckTime = MonoTime.currTime();
|
auto lastCheckTime = MonoTime.currTime();
|
||||||
while (true) {
|
while (true) {
|
||||||
m.update();
|
m.update(online);
|
||||||
auto currTime = MonoTime.currTime();
|
auto currTime = MonoTime.currTime();
|
||||||
if (currTime - lastCheckTime > checkInterval) {
|
if (currTime - lastCheckTime > checkInterval) {
|
||||||
lastCheckTime = currTime;
|
lastCheckTime = currTime;
|
||||||
performSync(sync);
|
online = testNetwork();
|
||||||
// discard all events that may have been generated by the sync
|
if (online) {
|
||||||
m.update(false);
|
performSync(sync);
|
||||||
|
// discard all events that may have been generated by the sync
|
||||||
|
m.update(false);
|
||||||
|
}
|
||||||
GC.collect();
|
GC.collect();
|
||||||
} else {
|
} else {
|
||||||
Thread.sleep(dur!"msecs"(100));
|
Thread.sleep(dur!"msecs"(100));
|
||||||
|
|
|
@ -63,7 +63,6 @@ final class OneDriveApi
|
||||||
void setRefreshToken(string refreshToken)
|
void setRefreshToken(string refreshToken)
|
||||||
{
|
{
|
||||||
this.refreshToken = refreshToken;
|
this.refreshToken = refreshToken;
|
||||||
newToken();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://dev.onedrive.com/items/view_changes.htm
|
// https://dev.onedrive.com/items/view_changes.htm
|
||||||
|
|
23
src/util.d
23
src/util.d
|
@ -1,11 +1,16 @@
|
||||||
import std.conv, std.digest.crc, std.digest.digest, std.file, std.path;
|
import std.conv;
|
||||||
import std.regex, std.stdio, std.string: chomp;
|
import std.digest.crc;
|
||||||
|
import std.file;
|
||||||
|
import std.path;
|
||||||
|
import std.regex;
|
||||||
|
import std.socket;
|
||||||
|
import std.stdio;
|
||||||
|
import std.string;
|
||||||
|
|
||||||
private string deviceName;
|
private string deviceName;
|
||||||
|
|
||||||
static this()
|
static this()
|
||||||
{
|
{
|
||||||
import std.socket;
|
|
||||||
deviceName = Socket.hostName;
|
deviceName = Socket.hostName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,3 +71,15 @@ Regex!char wild2regex(const(char)[] pattern)
|
||||||
str ~= "$";
|
str ~= "$";
|
||||||
return regex(str, "i");
|
return regex(str, "i");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return true if the network connection is available
|
||||||
|
bool testNetwork()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
auto addr = new InternetAddress("login.live.com", 443);
|
||||||
|
auto socket = new TcpSocket(addr);
|
||||||
|
return socket.isAlive();
|
||||||
|
} catch (SocketException) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue