fixed issues of download folder not being updated promptly

This commit is contained in:
huangjx
2021-09-11 15:09:23 +08:00
parent cba9d22cc8
commit cff84cf0e5
4 changed files with 59 additions and 12 deletions

View File

@@ -35,9 +35,7 @@ class Aria2Controller extends Controller
$this->downloadDir = $this->settings->get('ncd_downloader_dir') ?? "/Downloads";
OC_Util::setupFS();
//$this->config = \OC::$server->getAppConfig();
$this->aria2 = $aria2;
$this->aria2->init();
$this->dbconn = new DBConn();
}
@@ -84,8 +82,8 @@ class Aria2Controller extends Controller
}
public function Update()
{
File::syncFolder($this->downloadDir);
return new JSONResponse([]);
$resp = File::syncFolder();
//return new JSONResponse($resp);
}
private function createActionItem($name, $path)
@@ -99,6 +97,7 @@ class Aria2Controller extends Controller
{
//$path = $this->request->getRequestUri();
$counter = $this->aria2->getCounters();
$this->Update();
switch (strtolower($path)) {
case "active":
$resp = $this->aria2->tellActive();

View File

@@ -361,9 +361,9 @@ user-agent=Transmission/2.77
seed-ratio=0.1
bt-seed-unverified=true
max-overall-upload-limit=1M
#on-download-complete=$this->onDownloadComplete
#on-download-error=$this->onDownloadError
#on-download-start=$this->onDownloadStart
#on-download-complete=
#on-download-error=
#on-download-start=
save-session=$this->sessionFile
input-file=$this->sessionFile
log=$this->logFile

View File

@@ -2,18 +2,30 @@
namespace OCA\NCDownloader\Tools;
use OCA\NCDownloader\Tools\Helper;
use OC\Files\Filesystem;
use OCA\NCDownloader\Tools\Settings;
use OC\Files\Utils\Scanner;
use \OCP\EventDispatcher\IEventDispatcher;
class File
{
public static function syncFolder($dir)
public static function syncFolder($dir = null)
{
$user = \OC::$server->getUserSession()->getUser()->getUID();
if (!isset($dir)) {
$settings = new Settings($user);
$downloadDir = $settings->get('ncd_downloader_dir') ?? "/Downloads";
$rootFolder = Helper::getUserFolder($user);
$path = $rootFolder . "/" . ltrim($downloadDir, '/\\');
} else {
$path = $dir;
}
$realDir =\OC::$server->getSystemConfig()->getValue('datadirectory') . "/" . $path;
if (!(Helper::folderUpdated($realDir))) {
return ['message' => "no change"];
}
$logger = \OC::$server->getLogger();
$scanner = new Scanner($user, \OC::$server->getDatabaseConnection(), \OC::$server->query(IEventDispatcher::class), $logger);
$path = Filesystem::getRoot() . "/" . ltrim($dir, '/\\');
try {
$scanner->scan($path);
// Helper::debug($logger->getLogPath());
@@ -24,5 +36,7 @@ class File
$logger->warning("Exception during scan: " . $e->getMessage() . $e->getTraceAsString());
}
return ['message' => "changed"];
}
}

View File

@@ -3,6 +3,7 @@
namespace OCA\NCDownloader\Tools;
use OCA\NCDownloader\Tools\aria2Options;
use OC\Files\Filesystem;
class Helper
{
@@ -253,5 +254,38 @@ class Helper
];
return $titles[$type];
}
// the relative home folder of a nextcloud user
public static function getUserFolder($uid = null)
{
if (!empty($rootFolder = Filesystem::getRoot())) {
return $rootFolder;
} else if (isset($uid)) {
return "/" . strtolower($uid) . "/files";
}
return '';
}
public static function folderUpdated($dir)
{
if (!file_exists($dir)) {
return false;
}
$checkFile = $dir . "/.lastmodified";
if (!file_exists($checkFile)) {
$time = \filemtime($dir);
file_put_contents($checkFile, $time);
return false;
}
$lastModified = (int) file_get_contents($checkFile);
$time = \filemtime($dir);
if ($time > $lastModified) {
file_put_contents($checkFile, $time);
return true;
}
return false;
}
}