-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
407 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2022 Varun Patil <radialapps@gmail.com> | ||
* @author Varun Patil <radialapps@gmail.com> | ||
* @license AGPL-3.0-or-later | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace OCA\Memories\Controller; | ||
|
||
use OCA\Memories\Util; | ||
use OCP\AppFramework\Http; | ||
use \OCA\Memories\Db\DevSync; | ||
|
||
class DevSyncController extends GenericApiController | ||
{ | ||
/** | ||
* @NoAdminRequired | ||
* | ||
* @PublicPage | ||
*/ | ||
public function register(int $deviceId, array $auids): Http\Response | ||
{ | ||
return Util::guardEx(static function () use ($deviceId, $auids) { | ||
return \OC::$server->get(DevSync::class)->register($deviceId, $auids); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Memories\Db; | ||
|
||
use OCA\Memories\Util; | ||
use OCP\IDBConnection; | ||
|
||
class DevSync | ||
{ | ||
protected IDBConnection $connection; | ||
|
||
public function __construct(IDBConnection $connection) | ||
{ | ||
$this->connection = $connection; | ||
} | ||
|
||
/** | ||
* Process an existing file for sync purposes. | ||
* | ||
* @param int $fileid The file ID | ||
* @param int $auid The AUID | ||
*/ | ||
public function haveFile(int $fileid, int $auid) | ||
{ | ||
// Get the current record for the file | ||
$query = $this->connection->getQueryBuilder(); | ||
$current = $query | ||
->select('*') | ||
->from('memories_sync') | ||
->where( | ||
$query->expr()->orX( | ||
$query->expr()->eq('fileid', $query->createNamedParameter($fileid)), | ||
$query->expr()->eq('auid', $query->createNamedParameter($auid)) | ||
), | ||
)->executeQuery() | ||
->fetch() | ||
; | ||
|
||
if ($current) { | ||
// Update status if it's not extant | ||
$updateStatus = 1 !== (int) $current['status']; | ||
|
||
// Update the file ID only if it didn't already exist | ||
// or if the file didn't exist in our collection to begin with | ||
$updateFileId = $updateStatus || 0 === (int) $current['fileid']; | ||
|
||
if (!$updateFileId && !$updateStatus) { | ||
// Nothing to do. This can also happen if same AUID exists | ||
// with a different file ID (duplicates get ignored) | ||
return; | ||
} | ||
|
||
// Update the record | ||
$query = $this->connection->getQueryBuilder(); | ||
$query->update('memories_sync') | ||
->set('mtime', $query->createNamedParameter(time())) | ||
->set('status', $query->createNamedParameter(1)) | ||
->where($query->expr()->eq('id', $query->createNamedParameter((int) $current['id']))) | ||
; | ||
|
||
// Only update the file ID if it didn't exist | ||
if ($updateFileId) { | ||
$query->set('fileid', $query->createNamedParameter($fileid)); | ||
} | ||
|
||
$query->executeStatement(); | ||
|
||
return; | ||
} | ||
|
||
// File does not exist in sync; create a new record | ||
$query = $this->connection->getQueryBuilder(); | ||
$query->insert('memories_sync') | ||
->values([ | ||
'mtime' => $query->createNamedParameter(time()), | ||
'fileid' => $query->createNamedParameter($fileid), | ||
'auid' => $query->createNamedParameter($auid), | ||
'device' => $query->createNamedParameter(0), | ||
'status' => $query->createNamedParameter(1), | ||
]) | ||
->executeStatement() | ||
; | ||
} | ||
|
||
/** | ||
* Process a deleted file for sync purposes. | ||
* | ||
* @param int $fileid The file ID | ||
*/ | ||
public function deleteFile(int $fileid) | ||
{ | ||
// If the file is being deleted, simply mark any existing record as such and return | ||
$query = $this->connection->getQueryBuilder(); | ||
$query->update('memories_sync') | ||
->set('mtime', $query->createNamedParameter(time())) | ||
->set('status', $query->createNamedParameter(2)) | ||
->where($query->expr()->eq('fileid', $query->createNamedParameter($fileid))) | ||
->executeStatement() | ||
; | ||
} | ||
|
||
/** | ||
* Register a device for a set of AUIDs. | ||
* | ||
* @param int $deviceId The device ID | ||
* @param array $auids The AUIDs | ||
*/ | ||
public function register(int $deviceId, array $auids) | ||
{ | ||
// Convert all auid to int and filter out zeros | ||
$auids = array_filter(array_map('intval', $auids)); | ||
|
||
$uid = Util::getUID(); | ||
|
||
// Result for mappings | ||
$result = []; | ||
|
||
// Get filesystem manager | ||
$fs = \OC::$server->get(\OCA\Memories\Db\FsManager::class); | ||
|
||
// Get the current records for the AUIDs | ||
foreach ($auids as $auid) { | ||
$query = $this->connection->getQueryBuilder(); | ||
$current = $query | ||
->select('*') | ||
->from('memories_sync') | ||
->where($query->expr()->eq('auid', $query->createNamedParameter($auid))) | ||
->executeQuery() | ||
->fetch() | ||
; | ||
|
||
// An existing record exists | ||
if ($current) { | ||
$recordId = (int) $current['id']; | ||
|
||
$result[] = [ | ||
'fileid' => (int) $current['fileid'], | ||
'auid' => (int) $current['auid'], | ||
'status' => (int) $current['status'], | ||
]; | ||
|
||
// Update the record if device is not registered | ||
if (!(int) $current['device']) { | ||
$this->updateDeviceId($deviceId, $recordId); | ||
} | ||
|
||
// If a different device is registered the file owner gets priority | ||
elseif ((int) $current['device'] !== $deviceId) { | ||
$userFile = $fs->getUserFolderFile((int) $current['fileid']); | ||
|
||
// If the owner is the current user, update the device ID | ||
if (null !== $userFile && $userFile->getOwner() && $userFile->getOwner()->getUID() === $uid) { | ||
$this->updateDeviceId($deviceId, $recordId); | ||
} | ||
} | ||
|
||
continue; | ||
} | ||
|
||
// Create record | ||
$query = $this->connection->getQueryBuilder(); | ||
$query->insert('memories_sync') | ||
->values([ | ||
'mtime' => $query->createNamedParameter(time()), | ||
'fileid' => $query->createNamedParameter(0), | ||
'auid' => $query->createNamedParameter($auid), | ||
'device' => $query->createNamedParameter($deviceId), | ||
'status' => $query->createNamedParameter(0), | ||
]) | ||
->executeStatement() | ||
; | ||
|
||
// Add to result | ||
$result[] = [ | ||
'fileid' => 0, | ||
'auid' => (int) $current['auid'], | ||
'status' => 0, | ||
]; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Update the device ID for a file. | ||
* | ||
* @param int $recordId The record ID | ||
* @param int $deviceId The device ID | ||
*/ | ||
private function updateDeviceId(int $recordId, int $deviceId) | ||
{ | ||
$query = $this->connection->getQueryBuilder(); | ||
$query->update('memories_sync') | ||
->set('device', $query->createNamedParameter($deviceId)) | ||
->where($query->expr()->eq('id', $query->createNamedParameter($recordId))) | ||
->executeStatement() | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.