Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/5.3-develop' into 5.3-develop
Browse files Browse the repository at this point in the history
  • Loading branch information
brookgagnon committed Nov 25, 2024
2 parents 4871f28 + 217437f commit 8bc2ec5
Show file tree
Hide file tree
Showing 10 changed files with 358 additions and 7 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.3.3-20241113
5.3.3-20241125
4 changes: 3 additions & 1 deletion classes/core/obfhelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static function image_format($filename)
* @param width Target width.
* @param height Target height.
*/
public static function image_resize($src, $dst, $width, $height)
public static function image_resize($src, $dst, $width, $height, $rotate = 0)
{
if (!file_exists($src)) {
trigger_error('The source file does not exist', E_USER_WARNING);
Expand Down Expand Up @@ -208,6 +208,8 @@ public static function image_resize($src, $dst, $width, $height)
$im->setImageBackgroundColor('white'); // Set white background if necessary
$im->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE); // Remove alpha channel
$im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN); // Flatten image
$im->rotateImage('white', $rotate);

$im->thumbnailImage($width, $height, true); // Adjust width and height as necessary (maintain aspect ratio)

// Set the image format and apply lossy compression
Expand Down
43 changes: 43 additions & 0 deletions controllers/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,49 @@ public function save()
}
}

/**
* Save additional media properties.
*
* @param id
*
* @route POST /v2/media/properties/(:id:)
*/
public function save_properties()
{
$this->user->require_authenticated();

$id = $this->data('id');
$properties = $this->data('properties');

$media = $this->models->media('get_by_id', ['id' => $id]);

// trigger permission failure if can't edit
if (!$this->user_can_edit($media)) {
$this->user->require_permission('manage_media');
}

$this->models->media('properties', ['id' => $id, 'properties' => $properties]);

return [true, 'Properties saved.'];
}

/**
* Get additional media properties.
*
* @param id
*
* @route GET /v2/media/properties/(:id:)
*/
public function get_properties()
{
$this->user->require_authenticated();

$id = $this->data('id');
$properties = $this->models->media('properties', ['id' => $id]);

return [true, 'Properties.', $properties];
}

/**
* Checks that user can manage versions for media item. Does not return true
* or false, but can throw a permissions error using 'require_permissions'.
Expand Down
18 changes: 16 additions & 2 deletions html/media/details.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<ob-tabs>
<ob-tab data-name="Details">

<div style="display: flex; justify-content: space-between;">
<div>
<h1 id="media_heading" data-t>Media Details</h1>
Expand Down Expand Up @@ -91,7 +90,6 @@ <h1 id="media_heading" data-t>Media Details</h1>
</div>

</div>

</fieldset>
</ob-tab>
<ob-tab data-name="Where Used">
Expand All @@ -101,4 +99,20 @@ <h1>Where is this media used?</h1>
</ul>
</div>
</ob-tab>
<ob-tab data-name="Settings">
<h1>Media Settings</h1>
<div id="media_details_settings_saved" class="obwidget message hidden success" data-type="message">Settings saved.
</div>
<div class="media_details_settings" hidden data-type="image">
<h2>Image Rotate</h2>
</div>
<div class="media_details_settings" hidden data-type="none">
<p>There are currently no settings available for this media type.</p>
</div>
<fieldset class="media_details_settings_save" hidden>
<div class="fieldrow">
<button class="add" onclick="OB.MediaDetails.saveSettings()">Save</button>
</div>
</fieldset>
</ob-tab>
</ob-tabs>
49 changes: 48 additions & 1 deletion js/media/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
*/

// media details page
OB.Media.detailsPage = function (id) {
OB.MediaDetails = {};
OB.MediaDetails.currentId = null;
OB.MediaDetails.page = function (id) {
OB.MediaDetails.currentId = id;
OB.API.post("media", "get", { id: id, where_used: true }, function (response) {
if (response.status == false) return;

Expand All @@ -27,6 +30,26 @@ OB.Media.detailsPage = function (id) {
var item = response.data;
var used = response.data.where_used.used;

if (item?.type == "image") {
OB.API.post("media", "get_properties", { id: id }, function (propertiesResponse) {
const properties = propertiesResponse.data;

const mediaDetailsSettings = document.querySelector('.media_details_settings[data-type="image"]');
mediaDetailsSettings.removeAttribute("hidden");
const imageRotate = document.createElement("ob-field-image-rotate");
// set class
imageRotate.className = "media_details_settings_imagerotate";
imageRotate.dataset.edit = "true";
imageRotate.dataset.id = item.id;
console.log(properties);
imageRotate.value = imageRotate.offset = properties?.rotate ?? 0;
mediaDetailsSettings.appendChild(imageRotate);
document.querySelector(".media_details_settings_save").removeAttribute("hidden");
});
} else {
$('.media_details_settings[data-type="none"]').show();
}

// handle buttons

// we can download if we're the owner, or we have the download_media permission
Expand Down Expand Up @@ -223,3 +246,27 @@ OB.Media.detailsPage = function (id) {
$("#media_details_used").show();
});
};

OB.MediaDetails.saveSettings = async function () {
$("#media_details_settings_saved").hide();

const rotateField = document.querySelector(".media_details_settings_imagerotate");

if (rotateField) {
const data = {};
data.properties = {
rotate: rotateField.value,
};

OB.API.post(
"media",
"save_properties",
{ id: OB.MediaDetails.currentId, properties: data.properties },
function (response) {
if (response.status == true) {
$("#media_details_settings_saved").show();
}
},
);
}
};
2 changes: 1 addition & 1 deletion js/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ OB.Sidebar.contextMenuDownload = function (id) {
};

OB.Sidebar.contextMenuDetailsPage = function (id) {
OB.Media.detailsPage(id);
OB.MediaDetails.page(id);
};

OB.Sidebar.contextMenuVersionPage = function (id, title) {
Expand Down
86 changes: 85 additions & 1 deletion models/media_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public function get_init_what($args = [])
$this->db->what('media.status', 'status');
$this->db->what('media.dynamic_select', 'dynamic_select');
$this->db->what('users.display_name', 'owner_name');
$this->db->what('media.properties', 'properties');

foreach ($args['metadata_fields'] as $metadata_field) {
// add the field to the select portion of the query
Expand Down Expand Up @@ -334,6 +335,13 @@ public function thumbnail_file($args = [])
return false;
}

$media_properties = json_decode($media['properties'], true);
if ($media_properties && $media_properties['rotate']) {
$rotate = $media_properties['rotate'];
} else {
$rotate = null;
}

// first search for a cached version of our resized thumbnail
$cache_directory = OB_CACHE . '/thumbnails/media/' . $media['file_location'][0] . '/' . $media['file_location'][1];
$thumbnail_files = glob($cache_directory . '/' . $media['id'] . '.*');
Expand Down Expand Up @@ -365,7 +373,7 @@ public function thumbnail_file($args = [])
$output_file = $output_dir . '/' . $media['id'] . '.webp';

// resize our image to a webp thumbnail
OBFHelpers::image_resize($input_file, $output_file, 600, 600);
OBFHelpers::image_resize($input_file, $output_file, 600, 600, $rotate);

// return our file if it exists now
if (file_exists($output_file)) {
Expand All @@ -376,6 +384,43 @@ public function thumbnail_file($args = [])
return false;
}

/**
* Clear thumbnail cache.
*
* @param media ID or media row array.
*/
public function thumbnail_clear($args = [])
{
OBFHelpers::require_args($args, ['media']);

if (!is_array($args['media'])) {
$this->db->where('id', $args['media']);
$media = $this->db->get_one('media');

if (!$media) {
return false;
}
} else {
$media = $args['media'];
}

OBFHelpers::require_args($media, ['type', 'is_archived', 'is_approved', 'file_location']);
if (strlen($media['file_location']) != 2) {
trigger_error('Invalid media file location.', E_USER_WARNING);
return false;
}

// first search for a cached version of our resized thumbnail
$cache_directory = OB_CACHE . '/thumbnails/media/' . $media['file_location'][0] . '/' . $media['file_location'][1];
$thumbnail_files = glob($cache_directory . '/' . $media['id'] . '.*');

foreach ($thumbnail_files as $thumbnail_file) {
unlink($thumbnail_file);
}

return true;
}

/**
* Get permissions linked to a media item.
*
Expand Down Expand Up @@ -1306,6 +1351,45 @@ public function validate($args = [])
return [true,$item['local_id']];
}

/**
* Get or set a media property.
*
* @param item
*
* @return id
*/
public function properties($args = [])
{
OBFHelpers::require_args($args, ['id']);
$media_id = $args['id'];
$properties = $args['properties'] ?? null;


$this->db->where('id', $media_id);
$media = $this->db->get_one('media');

if (!$media) {
return false;
}

if ($properties) {
$this->thumbnail_clear(['media' => $media_id]);
$this->db->where('id', $media_id);
return $this->db->update('media', ['properties' => json_encode($properties)]);
} else {
$properties = $media['properties'];

if ($properties) {
$properties = json_decode($properties, true);
} else {
$properties = [];
}

return $properties;
}
}


/**
* Insert or update a media item.
*
Expand Down
10 changes: 10 additions & 0 deletions routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
"media",
"search"
],
[
"/api/v2/media/properties/(:id:)",
"media",
"get_properties"
],
[
"/api/v2/media/versions/(:media_id:)",
"media",
Expand Down Expand Up @@ -357,6 +362,11 @@
"media",
"save"
],
[
"/api/v2/media/properties/(:id:)",
"media",
"save_properties"
],
[
"/api/v2/media/versions",
"media",
Expand Down
Loading

0 comments on commit 8bc2ec5

Please sign in to comment.