Jump to content

Remove uploaded files when plugin is uninstalled


shahed

Recommended Posts

I'm working on a plugin that use upload form field in two areas. first there is a plugin setting and with this plugin also comes widget feature that also uses upload form. both works, but in time of uninstall the plugin, the stored files must also be removed from server. now for upload form with plugin setting, I created an uninstall.php with this code:

if ( isset( \IPS\Settings::i()->plugin_uploadForm ) )
{
	\IPS\File::get( 'core_Theme', \IPS\Settings::i()->plugin_uploadForm )->delete();
}

this code works and stored file in plugin setting will delete after uninstall. however how can i do the same for widgets?

I use this for upload form itself in widgets:

if ( ! empty( $this->out['widget_uploadForm'] ) )
{
	$image = \IPS\File::get( 'core_Theme', $this->out['widget_uploadForm'] );
}
$form->add( new \IPS\Helpers\Form\Upload('widget_uploadForm', $image, FALSE, array( 'multiple' => false, 'storageExtension' => 'core_Theme', 'image' => true ), null, null, null, 'widget_uploadForm_toggle' ) );

 

Link to comment
Share on other sites

44 minutes ago, Adriano Faria said:

You should be using your own file storage extension to point to the right files.

Not sure can be done in plugins. not sure even it is a right approach but I tried to extend  \IPS\core\extensions\core\FileStorage\Theme with hook and add the delete code but didn't work.

	/**
	 * Delete all stored files
	 *
	 * @return	void
	 */
	public function delete()
	{
		\IPS\File::get( 'core_Theme', $this->configuration['widget_uploadForm'] )->delete();
		return parent::delete();
	}

 

36 minutes ago, DawPi said:

So you must convert it into an app. 🙂

Yes that would be ideal, but it is also time consuming to start over 🙃

Edited by shahed
Link to comment
Share on other sites

Stu is correct, in 99,9999% we'll reject it, BUT it's also OK to use an existing storage handler like core_Theme in few cases like uploading a watermark image, logo or any other use cases where it's not worth to have an own file storage extension and database table and all the other logic, as long as you take care of ALL the logic like removing the file when the plugin is removed, updating the path when the files were moved or the storage handler was changed.

We have also logic in place ( see Application::uploadSettings() ) to register the settings so that the framework takes care of everything.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...