tidying up

This commit is contained in:
huangjx
2022-07-24 10:54:02 +08:00
parent 96c7128ef7
commit 3a00325042
32 changed files with 146 additions and 144 deletions

148
lib/Db/Helper.php Normal file
View File

@@ -0,0 +1,148 @@
<?php
namespace OCA\NCDownloader\Db;
use OCA\NCDownloader\Tools\Helper as ToolsHelper;
class Helper
{
//@var OC\DB\ConnectionAdapter
private $conn;
private $table = "ncdownloader_info";
public function __construct()
{
$this->conn = \OC::$server->getDatabaseConnection();
$this->queryBuilder = $this->conn->getQueryBuilder();
$this->prefixedTable = $this->queryBuilder->getTableName($this->table);
//$container = \OC::$server->query(\OCP\IServerContainer::class);
//ToolsHelper::debug(get_class($container->query(\OCP\RichObjectStrings\IValidator::class)));
//$this->conn = \OC::$server->query(Connection::class);//working only with 22
//$this->connAdapter = \OC::$server->getDatabaseConnection();
//$this->conn = $this->connAdapter->getInner();
}
public function insert($insert)
{
$inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*' . $this->table, $insert, [
'gid',
]);
return $inserted;
}
public function getAll()
{
//OC\DB\QueryBuilder\QueryBuilder
$queryBuilder = $this->queryBuilder
->select('filename', 'type', 'gid', 'timestamp', 'status')
->from($this->table)
->execute();
return $queryBuilder->fetchAll();
}
public function getByUid($uid)
{
$queryBuilder = $this->queryBuilder
->select('*')
->from($this->table)
->where('uid = :uid')
->setParameter('uid', $uid)
->execute();
return $queryBuilder->fetchAll();
}
public function getUidByGid($gid)
{
$queryBuilder = $this->queryBuilder
->select('uid')
->from($this->table)
->where('gid = :gid')
->setParameter('gid', $gid)
->execute();
return $queryBuilder->fetchColumn();
}
public function getYtdlByUid($uid)
{
$qb = $this->queryBuilder
->select('*')
->from($this->table)
->where('uid = :uid')
->andWhere('type = :type')
->setParameter('uid', $uid)
->setParameter('type', ToolsHelper::DOWNLOADTYPE['YOUTUBE-DL'])
->orderBy('id', 'DESC')
->execute();
return $qb->fetchAll();
}
public function getByGid($gid)
{
$queryBuilder = $this->queryBuilder
->select('*')
->from($this->table)
->where('gid = :gid')
->setParameter('gid', $gid)
->execute();
return $queryBuilder->fetch();
}
public function save(array $keys, $values = array(), $conditions = array())
{
return $this->conn->setValues($this->table, $keys, $values, $conditions);
}
public function deleteByGid($gid)
{
$qb = $this->queryBuilder
->delete($this->table)
->where('gid = :gid')
->setParameter('gid', $gid);
return $qb->execute();
}
public function executeUpdate($sql, $values)
{
return $this->conn->executeUpdate($sql, $values);
}
public function updateStatus($gid, $status = 1)
{
$query = $this->queryBuilder;
$query->update($this->table)
->set("status", $query->createNamedParameter($status))
->where('gid = :gid')
->setParameter('gid', $gid);
return $query->execute();
//$sql = sprintf("UPDATE %s set status = ? WHERE gid = ?", $this->prefixedTable);
//$this->execute($sql, [$status, $gid]);
}
public function updateFilename($gid, $filename)
{
$query = $this->queryBuilder;
$query->update($this->table)
->set("filename", $query->createNamedParameter($filename))
->where('gid = :gid')
->andWhere('filename = :filename')
->setParameter('gid', $gid)
->setParameter('filename', 'unknown');
return $query->execute();
}
public function getDBType(): string
{
return \OC::$server->getConfig()->getSystemValue('dbtype', "mysql");
}
public function getExtra($data)
{
if ($this->getDBType() == "pgsql" && is_resource($data)) {
if (function_exists("pg_unescape_bytea")) {
$extra = pg_unescape_bytea(stream_get_contents($data));
}
else {
$extra = stream_get_contents($data);
}
return unserialize($extra);
}
return unserialize($data);
}
}

117
lib/Db/Settings.php Normal file
View File

@@ -0,0 +1,117 @@
<?php
namespace OCA\NCDownloader\Db;
use OC\AllConfig;
class Settings extends AllConfig
{
//@config OC\AppConfig
private $appConfig;
//@OC\SystemConfig
private $sysConfig;
//@OC\AllConfig
private $allConfig;
private $user;
private $appName;
//type of settings (system = 1 or app =2)
private $type;
private static $instance = null;
public const TYPE = ['SYSTEM' => 1, 'USER' => 2, 'APP' => 3];
public function __construct($user = null)
{
$this->appConfig = \OC::$server->getAppConfig();
$this->sysConfig = \OC::$server->getSystemConfig();
$this->appName = 'ncdownloader';
$this->type = self::TYPE['USER'];
$this->user = $user;
$this->allConfig = new AllConfig($this->sysConfig);
//$this->connAdapter = \OC::$server->getDatabaseConnection();
//$this->conn = $this->connAdapter->getInner();
}
public static function create($user = null)
{
if (!self::$instance) {
self::$instance = new static($user);
}
return self::$instance;
}
public function setType($type)
{
$this->type = $type;
return $this;
}
public function get($key, $default = null)
{
if ($this->type == self::TYPE['USER'] && isset($this->user)) {
return $this->allConfig->getUserValue($this->user, $this->appName, $key, $default);
} else if ($this->type == self::TYPE['SYSTEM']) {
return $this->allConfig->getSystemValue($key, $default);
} else {
return $this->allConfig->getAppValue($this->appName, $key, $default);
}
}
public function getAria2()
{
$settings = $this->allConfig->getUserValue($this->user, $this->appName, "custom_aria2_settings", '');
return json_decode($settings, 1);
}
public function getYtdl()
{
$settings = $this->get("custom_ytdl_settings");
return json_decode($settings, 1);
}
public function getAll()
{
if ($this->type === self::TYPE['APP']) {
return $this->getAllAppValues();
} else {
$data = $this->getAllUserSettings();
return $data;
}
}
public function save($key, $value)
{
try {
if ($this->type == self::TYPE['USER'] && isset($this->user)) {
$this->allConfig->setUserValue($this->user, $this->appName, $key, $value);
} else if ($this->type == self::TYPE['SYSTEM']) {
$this->allConfig->setSystemValue($key, $value);
} else {
$this->allConfig->setAppValue($this->appName, $key, $value);
}
} catch (\Exception $e) {
return ['error' => $e->getMessage];
}
return ['message' => "Saved!"];
}
public function getAllAppValues()
{
$keys = $this->getAllKeys();
$value = [];
foreach ($keys as $key) {
$value[$key] = $this->allConfig->getAppValue($this->appName, $key);
}
return $value;
}
public function getAllKeys()
{
return $this->allConfig->getAppKeys($this->appName);
}
public function getAllUserSettings()
{
$keys = $this->allConfig->getUserKeys($this->user, $this->appName);
$value = [];
foreach ($keys as $key) {
$value[$key] = $this->allConfig->getUserValue($this->user, $this->appName, $key);
}
return $value;
}
}