Skip to content

Commit

Permalink
tagging: move mimetype to metadata, add orphaned tag
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhilton committed Aug 23, 2024
1 parent d0f301b commit 340ee7f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 76 deletions.
24 changes: 18 additions & 6 deletions classes/local/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use stdClass;
use tool_objectfs\local\store\object_file_system;
use tool_objectfs\local\tag\tag_manager;

/**
* [Description manager]
Expand Down Expand Up @@ -160,7 +161,7 @@ public static function update_object_by_hash($contenthash, $newlocation, $filesi
$newobject->filesize = isset($oldobject->filesize) ? $oldobject->filesize :
$DB->get_field('files', 'filesize', ['contenthash' => $contenthash], IGNORE_MULTIPLE);

return self::update_object($newobject, $newlocation);
return self::upsert_object($newobject, $newlocation);
}
$newobject->location = $newlocation;

Expand All @@ -173,9 +174,7 @@ public static function update_object_by_hash($contenthash, $newlocation, $filesi
$newobject->filesize = $filesize;
$newobject->timeduplicated = time();
}
$DB->insert_record('tool_objectfs_objects', $newobject);

return $newobject;
return self::upsert_object($newobject, $newlocation);
}

/**
Expand All @@ -185,16 +184,29 @@ public static function update_object_by_hash($contenthash, $newlocation, $filesi
* @return stdClass
* @throws \dml_exception
*/
public static function update_object(stdClass $object, $newlocation) {
public static function upsert_object(stdClass $object, $newlocation) {
global $DB;

// If location change is 'duplicated' we update timeduplicated.
if ($newlocation === OBJECT_LOCATION_DUPLICATED) {
$object->timeduplicated = time();
}

$locationchanged = !isset($object->location) || $object->location != $newlocation;
$object->location = $newlocation;
$DB->update_record('tool_objectfs_objects', $object);

// If id is set, update, else insert new.
if (empty($object->id)) {
$object->id = $DB->insert_record('tool_objectfs_objects', $object);
} else {
$DB->update_record('tool_objectfs_objects', $object);
}

// Post update, notify tag manager since the location tag likely needs changing.
if ($locationchanged && tag_manager::is_tagging_enabled_and_supported()) {
$fs = get_file_storage()->get_file_system();
$fs->push_object_tags($object->contenthash);
}

return $object;
}
Expand Down
2 changes: 1 addition & 1 deletion classes/local/object_manipulator/manipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function execute(array $objectrecords) {

$newlocation = $this->manipulate_object($objectrecord);
if (!empty($objectrecord->id)) {
manager::update_object($objectrecord, $newlocation);
manager::upsert_object($objectrecord, $newlocation);
} else {
manager::update_object_by_hash($objectrecord->contenthash, $newlocation);
}
Expand Down
16 changes: 16 additions & 0 deletions classes/local/store/object_file_system.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ protected function get_local_path_from_hash($contenthash, $fetchifnotfound = fal
return $path;
}

/**
* Returns mimetype for a given hash
* @param string $contenthash
* @return string mimetype as stored in mdl_files
*/
protected function get_mimetype_from_hash(string $contenthash): string {
global $DB;
// We limit 1 because multiple files can have the same contenthash.
// However, they all have the same mimetype so it does not matter which one we query.
return $DB->get_field_sql('SELECT mimetype
FROM {files}
WHERE contenthash = :hash
LIMIT 1',
['hash' => $contenthash]);
}

/**
* get_remote_path_from_storedfile
* @param \stored_file $file
Expand Down
10 changes: 8 additions & 2 deletions classes/local/store/s3/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public function define_client_section($settings, $config) {
*
* @throws \Exception if fails.
*/
public function upload_to_s3($localpath, $contenthash) {
public function upload_to_s3($localpath, $contenthash, string $mimetype) {
$filehandle = fopen($localpath, 'rb');

if (!$filehandle) {
Expand All @@ -511,7 +511,13 @@ public function upload_to_s3($localpath, $contenthash) {
$uploader = new \Aws\S3\ObjectUploader(
$this->client, $this->bucket,
$this->bucketkeyprefix . $externalpath,
$filehandle
$filehandle,
'private',
[
'params' => [
'ContentType' => $mimetype,
],
]
);
$uploader->upload();
fclose($filehandle);
Expand Down
4 changes: 3 additions & 1 deletion classes/local/store/s3/file_system.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ public function readfile(\stored_file $file) {
* @return bool
*/
public function copy_from_local_to_external($contenthash) {
global $DB;
$localpath = $this->get_local_path_from_hash($contenthash);
$mime = $this->get_mimetype_from_hash($contenthash);

try {
$this->get_external_client()->upload_to_s3($localpath, $contenthash);
$this->get_external_client()->upload_to_s3($localpath, $contenthash, $mime);
return true;
} catch (\Exception $e) {
$this->get_logger()->error_log(
Expand Down
13 changes: 13 additions & 0 deletions classes/local/tag/environment_source.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ private static function get_env(): ?string {
* @return string|null mime type for file.
*/
public function get_value_for_contenthash(string $contenthash): ?string {
global $DB;

// If object is orphaned, return 'orphaned', otherwise return the env.
$location = $DB->get_field_sql('SELECT location
FROM {tool_objectfs_objects}
WHERE contenthash = :hash
LIMIT 1',
['hash' => $contenthash]);

if ($location == OBJECT_LOCATION_ORPHANED) {
return 'orphan';
}

return self::get_env();
}
}
65 changes: 0 additions & 65 deletions classes/local/tag/mime_type_source.php

This file was deleted.

1 change: 0 additions & 1 deletion classes/local/tag/tag_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public static function get_defined_tag_sources(): array {
// All possible tag sources should be defined here.
// Note this should be a maximum of 10 sources, as this is an AWS limit.
return [
new mime_type_source(),
new environment_source(),
];
}
Expand Down

0 comments on commit 340ee7f

Please sign in to comment.