1

I'm using the OneUp Uploader Bundle, with the orphanage, and I'm unsure about how to handle cleaning up entities (that were created by the listener), after the file has been cleaned up by the 'clear-orphans' command - how is this commonly handled?

I'd love to see an event fired for each file that is cleaned up (passing the filename and the mapping) but haven't found anything (assuming the event dispatcher is available to a command).

dblack
  • 79
  • 8

1 Answers1

1

The idea of the orphanage in the OneupUploaderBundle is that you don't pollute your upload folder with files that don't belong to any entities. This implies that if you use the uploaded files in your entities you should move them out of the orphanage. If you configured a mapping to use the orphanage, all the uploaded files will be stored in a separate folder after uploading.

Citing the documentation:

They will be moved to the correct place as soon as you trigger the uploadFiles method on the Storage.

This means that you can move the logic out of the event listener (which will be called non the less), but move it to the controller where you want to finally upload and store the files in the orphanage.

// src/Acme/Controller/AcmeController.php

class AcmeController extends Controller
{
    public function storeAction()
    {

        // ...
        if ($form->isValid()) {
            $orphanageManager = $this->get('oneup_uploader.orphanage_manager')->get('gallery');

            // upload all files to the configured storage
            $files = $manager->uploadFiles();

            // your own logic to apply the files to the entity
            // ...
        }

    }
}

Be sure to only use the orphanage if you really have to. It has some drawbacks.

Community
  • 1
  • 1
devsheeep
  • 2,076
  • 1
  • 22
  • 31
  • Thanks for the thorough response! My problem is that i need to capture the original filename, which is really easy using the listener - capture the original file pre upload, capture the new filename post upload and store both values in an entity (it's this entity that will need deleting if the form is never submitted). I don't think it's possible to get the original from the orphanage manager. The orphanage is perfect for my particular use-case (which provides the ability to upload multiple attachments to a standalone form - which may never get submitted) – dblack Nov 13 '14 at 21:09
  • Regarding the issue about users getting files from a previous attempt at uploading files to a form, a simple workaround would be to clear the files uploaded for the session on a 'GET' request... How would you feel about a clear method on the FilesystemOrphanageStorage object (that cleans up all the files submitted to the session), that we could then call in the controller? – dblack Nov 13 '14 at 21:26