Skip to content

Commit

Permalink
fix: never delete media files (#1411)
Browse files Browse the repository at this point in the history
* feat: display db migration errors if they occur

implements #1274

* change: add active column to MediaFile model

* change(MediaFile): toggle active property instead of deleting

fixes #1410

* fix: add missing data migration

* chore: simplify namespaces
  • Loading branch information
eteubert committed Dec 26, 2023
1 parent 4abd033 commit b759576
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 154 deletions.
16 changes: 9 additions & 7 deletions includes/api/episodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public function get_item_media($request)
'asset' => $asset->title,
'url' => $file->get_file_url(),
'size' => $file->size,
'enable' => true,
'enable' => (bool) $file->active,
];
}, $assets);

Expand Down Expand Up @@ -806,11 +806,13 @@ public function update_item_media_enable($request)

$file = MediaFile::find_or_create_by_episode_id_and_episode_asset_id($episode->id, $asset_id);
$file->determine_file_size();
$file->active = true;
$file->save();

if ($file->size == 0) {
return new \Podlove\Api\Response\OkResponse([
'message' => 'file size cannot be determined',
'active' => $file->active,
'status' => 'ok'
]);
}
Expand All @@ -820,6 +822,7 @@ public function update_item_media_enable($request)
'status' => 'ok',
'file_size' => $file->size,
'file_url' => $file->get_file_url(),
'active' => $file->active
]);
}

Expand All @@ -834,18 +837,15 @@ public function update_item_media_disable($request)

$file = MediaFile::find_by_episode_id_and_episode_asset_id($episode->id, $asset_id);
if ($file) {
// FIXME: Problem: When deleting a media file, all associated
// downloads are lost. We should never ever delete a media file.
// Maybe add a boolean flag to it instead that remembers if the
// checkbox is on or off.
// @see https://github.com/podlove/podlove-publisher/issues/1410
$file->delete();
$file->active = false;
$file->save();
}

return new \Podlove\Api\Response\OkResponse([
'status' => 'ok',
'file_size' => $file->size,
'file_url' => $file->get_file_url(),
'active' => $file->active,
]);
}

Expand All @@ -867,6 +867,7 @@ public function update_item_media_verify($request)
'status' => 'ok',
'message' => 'file size cannot be determined',
'file_url' => $file->get_file_url(),
'active' => $file->active,
]);
}
do_action('podlove_media_file_content_verified', $file->id);
Expand All @@ -875,6 +876,7 @@ public function update_item_media_verify($request)
'status' => 'ok',
'file_size' => $file->size,
'file_url' => $file->get_file_url(),
'active' => $file->active,
]);
}

Expand Down
71 changes: 71 additions & 0 deletions includes/db_migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* Execute migration query. Captures error if one occurs.
*
* @param string $sql
*/
function podlove_do_migration_query($sql)
{
global $wpdb;

$success = $wpdb->query($sql);

if ($success === false) {
update_option('podlove_db_migration_error', [
'error' => $wpdb->last_error,
'query' => $wpdb->last_query,
]);
}

return (bool) $success;
}

add_action('admin_notices', 'podlove_show_database_migration_error');

function podlove_show_database_migration_error()
{
$data = get_option('podlove_db_migration_error');

if (!$data || !isset($data['error']) || !isset($data['query'])) {
return;
}

if (isset($_REQUEST['podlove_hide_migration_error']) && $_REQUEST['podlove_hide_migration_error']) {
delete_option('podlove_db_migration_error');

return;
}

?>
<div class="notice notice-error">
<p>
<?php echo __('An error occurred during a Podlove Podcast Publisher database migration.', 'podlove-podcasting-plugin-for-wordpress'); ?>
</p>
<p>
<?php echo __('Error', 'podlove-podcasting-plugin-for-wordpress'); ?>: <code><?php echo esc_html($data['error']); ?></code>
</p>
<p>
<?php echo __('Query', 'podlove-podcasting-plugin-for-wordpress'); ?>: <code><?php echo esc_html($data['query']); ?></code>
</p>
<p>
<?php echo sprintf(
__('The plugin might not fully work until this is resolved. If you do not know what to do, ask for help in the forums: %s', 'podlove-podcasting-plugin-for-wordpress'),
'<a href="https://community.podlove.org/" target="_blank">community.podlove.org</a>'
); ?>
</p>
<p>
<a href="<?php echo podlove_hide_migration_error_url(); ?>"><?php echo __('hide this message', 'podlove-podcasting-plugin-for-wordpress'); ?></a>
</p>
</div>
<?php
}

function podlove_hide_migration_error_url()
{
if (isset($_REQUEST['page']) && $_REQUEST['page']) {
return admin_url('admin.php?page='.$_REQUEST['page'].'&podlove_hide_migration_error=1');
}

return admin_url('?podlove_hide_migration_error=1');
}
2 changes: 2 additions & 0 deletions lib/model/media_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public static function find_or_create_by_episode_id_and_episode_asset_id($episod
$file = new MediaFile();
$file->episode_id = $episode_id;
$file->episode_asset_id = $episode_asset_id;
$file->active = true;
$file->save();
}

Expand Down Expand Up @@ -424,3 +425,4 @@ private function validate_request($response)
MediaFile::property('episode_asset_id', 'INT');
MediaFile::property('size', 'INT');
MediaFile::property('etag', 'VARCHAR(255)');
MediaFile::property('active', 'TINYINT');

0 comments on commit b759576

Please sign in to comment.