Skip to content

Commit

Permalink
Properly delete images when they are deleted in an admin setting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese committed Feb 20, 2017
1 parent f9d6559 commit eb00d3a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
31 changes: 16 additions & 15 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,34 @@ function theme_photo_update_settings_images($settingname) {
// This is the component name the setting is stored in.
$component = 'theme_photo';


// This is the value of the admin setting which is the filename of the uploaded file.
$filename = get_config($component, $settingname);
// We extract the file extension because we want to preserve it.
$extension = substr($filename, strrpos($filename, '.') + 1);

// This is the path in the moodle internal file system.
$fullpath = "/{$syscontext->id}/{$component}/{$settingname}/0{$filename}";

// This location matches the searched for location in theme_config::resolve_image_location.
$pathname = $CFG->dataroot . '/pix_plugins/theme/photo/' . $settingname . '.' . $extension;

// This pattern matches any previous files with maybe different file extensions.
$pathpattern = $CFG->dataroot . '/pix_plugins/theme/photo/' . $settingname . '.*';

// Make sure this dir exists.
@mkdir($CFG->dataroot . '/pix_plugins/theme/photo/', $CFG->directorypermissions, true);

// Delete any existing files for this setting.
foreach (glob($pathpattern) as $filename) {
@unlink($filename);
}

// Get an instance of the moodle file storage.
$fs = get_file_storage();
// This is an efficient way to get a file if we know the exact path.
if ($file = $fs->get_file_by_hash(sha1($fullpath))) {
// We got the stored file - copy it to dataroot.
// This location matches the searched for location in theme_config::resolve_image_location.
$pathname = $CFG->dataroot . '/pix_plugins/theme/photo/' . $settingname . '.' . $extension;

// This pattern matches any previous files with maybe different file extensions.
$pathpattern = $CFG->dataroot . '/pix_plugins/theme/photo/' . $settingname . '.*';

// Make sure this dir exists.
@mkdir($CFG->dataroot . '/pix_plugins/theme/photo/', $CFG->directorypermissions, true);

// Delete any existing files for this setting.
foreach (glob($pathpattern) as $filename) {
@unlink($filename);
}

// Copy the current file to this location.
$file->copy_content_to($pathname);
}

Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
defined('MOODLE_INTERNAL') || die();

// This is the version of the plugin.
$plugin->version = 2017010300;
$plugin->version = 2017022000;

// This is the version of Moodle this plugin requires.
$plugin->requires = 2016112900.00;
Expand All @@ -44,5 +44,5 @@
$plugin->maturity = MATURITY_STABLE;

// This is the named version.
$plugin->release = 1.2;
$plugin->release = 1.3;

0 comments on commit eb00d3a

Please sign in to comment.