memories/lib/Db/TimelineWrite.php

148 lines
5.3 KiB
PHP
Raw Normal View History

2022-08-20 02:53:21 +00:00
<?php
declare(strict_types=1);
namespace OCA\Memories\Db;
use OCA\Memories\AppInfo\Application;
use OCA\Memories\Exif;
use OCP\Files\File;
use OCP\IDBConnection;
2022-09-11 02:22:57 +00:00
use OCP\DB\QueryBuilder\IQueryBuilder;
2022-08-20 02:53:21 +00:00
class TimelineWrite {
2022-09-09 07:31:42 +00:00
protected IDBConnection $connection;
2022-08-20 02:53:21 +00:00
2022-09-09 07:31:42 +00:00
public function __construct(IDBConnection $connection) {
$this->connection = $connection;
}
2022-08-20 02:53:21 +00:00
/**
* Check if a file has a valid mimetype for processing
* @param File $file
* @return int 0 for invalid, 1 for image, 2 for video
*/
public function getFileType(File $file): int {
$mime = $file->getMimeType();
if (in_array($mime, Application::IMAGE_MIMES)) {
return 1;
} elseif (in_array($mime, Application::VIDEO_MIMES)) {
return 2;
}
return 0;
}
2022-08-20 02:53:21 +00:00
/**
* Process a file to insert Exif data into the database
* @param File $file
2022-08-23 09:35:51 +00:00
* @return int 2 if processed, 1 if skipped, 0 if not valid
2022-08-20 02:53:21 +00:00
*/
2022-09-13 17:39:38 +00:00
public function processFile(
File &$file,
bool $force=false,
string $fallbackUid=null
): int {
2022-08-20 02:53:21 +00:00
// There is no easy way to UPSERT in a standard SQL way, so just
// do multiple calls. The worst that can happen is more updates,
// but that's not a big deal.
// https://stackoverflow.com/questions/15252213/sql-standard-upsert-call
// Check if we want to process this file
$fileType = $this->getFileType($file);
$isvideo = ($fileType === 2);
if (!$fileType) {
2022-08-23 09:35:51 +00:00
return 0;
2022-08-20 02:53:21 +00:00
}
// Get parameters
$mtime = $file->getMtime();
$fileId = $file->getId();
2022-09-13 17:39:38 +00:00
$uid = $file->getOwner()->getUID();
if (empty($uid)) {
// Most likely this is an extrnal mount (that may be shared between users)
$uid = $fallbackUid;
}
if (empty($uid)) {
return 0;
}
2022-08-20 02:53:21 +00:00
// Check if need to update
2022-09-11 02:22:57 +00:00
$query = $this->connection->getQueryBuilder();
$query->select('fileid', 'mtime')
->from('memories')
->where(
$query->expr()->andX(
$query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)),
2022-09-13 17:39:38 +00:00
$query->expr()->eq('uid', $query->createNamedParameter($uid, IQueryBuilder::PARAM_STR)),
2022-09-11 02:22:57 +00:00
));
$prevRow = $query->executeQuery()->fetch();
if ($prevRow && !$force && intval($prevRow['mtime']) === $mtime) {
2022-08-23 09:35:51 +00:00
return 1;
2022-08-20 02:53:21 +00:00
}
// Get exif data
$exif = [];
try {
$exif = Exif::getExifFromFile($file);
2022-08-22 18:16:05 +00:00
} catch (\Exception $e) {}
2022-08-20 02:53:21 +00:00
// Get more parameters
$dateTaken = Exif::getDateTaken($file, $exif);
$dayId = floor($dateTaken / 86400);
$dateTaken = gmdate('Y-m-d H:i:s', $dateTaken);
if ($prevRow) {
// Update existing row
2022-09-11 02:22:57 +00:00
$query->update('memories')
->set('dayid', $query->createNamedParameter($dayId, IQueryBuilder::PARAM_INT))
->set('datetaken', $query->createNamedParameter($dateTaken, IQueryBuilder::PARAM_STR))
->set('mtime', $query->createNamedParameter($mtime, IQueryBuilder::PARAM_INT))
->set('isvideo', $query->createNamedParameter($isvideo, IQueryBuilder::PARAM_INT))
->where(
$query->expr()->andX(
$query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)),
2022-09-13 17:39:38 +00:00
$query->expr()->eq('uid', $query->createNamedParameter($uid, IQueryBuilder::PARAM_STR)),
2022-09-11 02:22:57 +00:00
));
$query->executeStatement();
2022-08-20 02:53:21 +00:00
} else {
2022-08-22 18:42:06 +00:00
// Try to create new row
try {
2022-09-11 02:22:57 +00:00
$query->insert('memories')
->values([
'fileid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
2022-09-13 17:39:38 +00:00
'uid' => $query->createNamedParameter($uid, IQueryBuilder::PARAM_STR),
2022-09-11 02:22:57 +00:00
'dayid' => $query->createNamedParameter($dayId, IQueryBuilder::PARAM_INT),
'datetaken' => $query->createNamedParameter($dateTaken, IQueryBuilder::PARAM_STR),
'mtime' => $query->createNamedParameter($mtime, IQueryBuilder::PARAM_INT),
'isvideo' => $query->createNamedParameter($isvideo, IQueryBuilder::PARAM_INT),
]);
$query->executeStatement();
2022-08-22 18:42:06 +00:00
} catch (\Exception $ex) {
error_log("Failed to create memories record: " . $ex->getMessage());
}
2022-08-20 02:53:21 +00:00
}
2022-08-23 09:35:51 +00:00
return 2;
2022-08-20 02:53:21 +00:00
}
/**
* Remove a file from the exif database
* @param File $file
*/
public function deleteFile(File &$file) {
2022-09-11 02:22:57 +00:00
$query = $this->connection->getQueryBuilder();
$query->delete('memories')
->where($query->expr()->eq('fileid', $query->createNamedParameter($file->getId(), IQueryBuilder::PARAM_INT)));
$query->executeStatement();
2022-08-20 02:53:21 +00:00
}
2022-09-11 00:15:40 +00:00
/**
* Clear the entire index. Does not need confirmation!
* @param File $file
*/
public function clear() {
2022-09-13 22:05:26 +00:00
$query = $this->connection->getQueryBuilder();
$query->delete('memories');
$query->executeStatement();
2022-09-11 00:15:40 +00:00
}
2022-08-20 02:53:21 +00:00
}