sroze / SRIORestUploadBundle

A symfony bundle to handle multiple upload ways on your REST API.
46 stars 17 forks source link

Writing file extension to file #10

Closed inverse closed 10 years ago

inverse commented 10 years ago

How would I go about appending the file extension to the uploaded files? I was thinking of having something like this in my Media model.

/**
 * Set the uploaded file instance.
 *
 * @param UploadedFile $file
 */
public function setFile(UploadedFile $file)
{
  // Add extension to file
  $guesser = ExtensionGuesser::getInstance();
  $filename = $file->getFilename() . '.' . $guesser->guess($file->getMimeType());

  $file->move($file->getPath(), $filename);

  $this->file = $file;
}

However $file->move() doesn't work to the isValid() failure on is_uploaded_file I think.

sroze commented 10 years ago

Hi @inverse ! The current master is currently not documented but support this: you simply have to implement your NamingStrategy and use it with the naming_strategy configuration.

You can have a look on the test app in Tests directory.

Note that the 2.0 version is using Gaufrette as storage helper so you can store in a local filesystem, or S3, or anything else :) I'll document this new version soon.

inverse commented 10 years ago

Thanks for getting back to me :)

Woah a lot has changed and I've not used Gaufrette before... looks interesting. I think I've got what I need need although when attempting to register my new NamingStrategy and running composer install I get a ServiceNotFoundException. Any ideas?

Error

 [Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
The service "srio_rest_upload.storage.default" has a dependency on a non-existent service "\menschdanke\apibundle\strategy\contenttypefileextnamingstrategy".

Config

# RestUploadBundle configuration
srio_rest_upload:
    storages:
        default:
            filesystem: gaufrette.default_filesystem
            naming_strategy: MenschDanke\ApiBundle\Strategy\ContentTypeFileExtNamingStrategy

    resumable_entity_class: SRIO\RestUploadBundle\Tests\Fixtures\Entity\ResumableUploadSession
sroze commented 10 years ago

The value of naming_strategy have to be a service id. That's way you can inject services you want to chose your file name if you need !

On Monday, September 22, 2014, Malachi Soord notifications@github.com wrote:

Thanks for getting back to me. Woah a lot has changed and I've not used Gaufrette before. I think I've got what I need need although when attempting to register my new NamingStrategy I get a ServiceNotFoundException.

my config looks like

RestUploadBundle configurationsrio_rest_upload:

storages:
    default:
        filesystem: gaufrette.default_filesystem
        naming_strategy: MenschDanke\ApiBundle\Strategy\ContentTypeFileExtNamingStrategy

resumable_entity_class: SRIO\RestUploadBundle\Tests\Fixtures\Entity\ResumableUploadSession``

— Reply to this email directly or view it on GitHub https://github.com/sroze/SRIORestUploadBundle/issues/10#issuecomment-56390499 .

inverse commented 10 years ago

Apologies for the constant bombardment of questions. I'm still getting the same error when I declare the naming strategy as a service by adding the following to my services.yml

    mensch_danke_api.storage.strategy:
        class: MenschDanke\ApiBundle\Strategy\ContentTypeFileExtNamingStrategy

and config.yml now looks like

# RestUploadBundle configuration
srio_rest_upload:
    storages:
        default:
            filesystem: gaufrette.default_filesystem
            naming_strategy: @mensch_danke_api.storage.strategy

    resumable_entity_class: SRIO\RestUploadBundle\Tests\Fixtures\Entity\ResumableUploadSession
C:\Code\MenschDanke\map>composer install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
Updating the "app/config/parameters.yml" file

  [Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
  The service "srio_rest_upload.storage.default" has a dependency on a non-existent service "@mensch_danke_api.storage.strategy
  ".

Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an
exception

  [RuntimeException]
  An error occurred when executing the ""cache:clear --no-warmup"" command.
sroze commented 10 years ago

Sure, why do you prefix by "@" ? Just the name of the service :-)

On Monday, September 22, 2014, Malachi Soord notifications@github.com wrote:

Apologies for the constant bombardment of questions. I'm still getting the same error when I declare the naming strategy as a service by adding the following to my services.yml

mensch_danke_api.storage.strategy:
    class: MenschDanke\ApiBundle\Strategy\ContentTypeFileExtNamingStrategy

and config.yml now looks like

RestUploadBundle configurationsrio_rest_upload:

storages:
    default:
        filesystem: gaufrette.default_filesystem
        naming_strategy: @mensch_danke_api.storage.strategy

resumable_entity_class: SRIO\RestUploadBundle\Tests\Fixtures\Entity\ResumableUploadSession

C:\Code\MenschDanke\map>composer install Loading composer repositories with package information Installing dependencies (including require-dev) from lock file Nothing to install or update Generating autoload files Updating the "app/config/parameters.yml" file

[Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException] The service "srio_rest_upload.storage.default" has a dependency on a non-existent service "@mensch_danke_api.storage.strategy ".

Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception

[RuntimeException] An error occurred when executing the ""cache:clear --no-warmup"" command.

— Reply to this email directly or view it on GitHub https://github.com/sroze/SRIORestUploadBundle/issues/10#issuecomment-56397355 .

inverse commented 10 years ago

I thought you had too.. Obviously not! All is working now! Thanks!