2017 © Pedro Peláez
 

symfony-bundle mass-media-bundle

File path management bundle for Symfony

image

south634/mass-media-bundle

File path management bundle for Symfony

  • Thursday, February 16, 2017
  • by south634
  • Repository
  • 1 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

MassMediaBundle

A Symfony bundle for creating hashed filenames from uploaded files, and spreading them out across directories., (*1)

You can set the hashing algorithm you want to use to create filenames, how many folders deep you want the files to be stored, and how many characters to use per folder name. It uses the leading characters in the hashed filename to create the folder names., (*2)

For example, given sha1 as your hashing algo, you might get a filename like this:, (*3)

3882be53dbfc4a0a4305fba989d224b863fe8cfd.jpg, (*4)

If you set folder character length: 2, and folder depth: 2, that file would be stored here:, (*5)

/38/82/3882be53dbfc4a0a4305fba989d224b863fe8cfd.jpg, (*6)

Setting folder character length: 3, and folder depth: 1, would store the file here:, (*7)

/388/3882be53dbfc4a0a4305fba989d224b863fe8cfd.jpg, (*8)

This functionality may be useful for those that do not want to have too many files residing in any single directory., (*9)

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command:, (*10)

$ composer require south634/mass-media-bundle "dev-master"

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation., (*11)

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:, (*12)

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new South634\MassMediaBundle\South634MassMediaBundle(),
        );

        // ...
    }

    // ...
}

Step 3: Add Settings

Finally, configure the required settings as you wish in the app/config/config.yml file of your project., (*13)

See example below:, (*14)

# app/config/config.yml

south634_mass_media:
    settings:
        hash_algo: sha1
        upload_dir: media        
        folder_depth: 2
        folder_chars: 2

Required Settings:, (*15)

hash_algo, (*16)

The hashing algorithm you want to use for filename creation. Must be an algo present in the array returned by the PHP hash_algos() function., (*17)

upload_dir, (*18)

Name of folder where you want to store the uploads. This folder will be created in your Symfony application's web accessible directory., (*19)

folder_depth, (*20)

How many folders deep you want to store the files. Minimum is 0., (*21)

folder_chars, (*22)

How many characters you want to use per folder name. Minimum is 0., (*23)

Note: folder_depth * folder_chars should never be greater than your expected filename length. For example, with hash_algo: sha1 you'd expect filename hashes 40 characters long. In that case, you should not set folder_depth: 20 and folder_chars: 3, because 20 * 3 > 40. Your filename would not have enough characters to create the folders., (*24)

Optional Settings:, (*25)

web_dir_name, (*26)

Name of the web accessible directory in your Symfony application. Defaults to 'web'., (*27)

root_dir, (*28)

Absolute path to the root directory of your Symfony application. Defaults to '%kernel.root_dir%'., (*29)

Usage

MassMediaManager

Get the MassMediaManager service in your Controller like so:, (*30)

$mmm = $this->get('south634_mass_media.manager');

Create a filename from UploadedFile (Prevent duplicate filenames)

$fileName = $mmm->getFileName($file);

This creates a hashed filename from an UploadedFile object., (*31)

MassMediaManager uses PHP's hash_file() function to create the filename here. That means that even if you upload two identical files which have different filenames, you should receive the same hashed filename for both of those files., (*32)

For example, say you have a cat photo, 'cute-cat.jpg'. You copy that photo elsewhere and rename it, 'kittypic.jpg'. Now, you upload both photos. As the images are still the same, the hash created by hash_file() will be the same them., (*33)

This could be useful if you want to cut down on duplicate files being stored on your server. Just be careful to check if a file is owned by any other entities before deleting it., (*34)

Create filename from UploadedFile (Allow duplicate files)

If you want to allow uploads of duplicate files, the getFileName() method accepts an optional $unique parameter which can be used to do so. For example, you could create a unique hashed filename for each user by adding the User's id to the file like so:, (*35)

$fileName = $mmm->getFileName($file, $user->getId());

Using the same example with the 2 identical cat photo files from above, imagine that User 1 uploads 'cute-cat.jpg', while User 2 uploads 'kittypic.jpg'. Even though it's an identical file, their unique user id is added to the hash, which results in a different filename that is unique to each user, and can be stored separately., (*36)

Create a Filename from URL

$fileName = $mmm->getFileNameFromUrl($url);

If you want to create a filename from a URL instead of an UploadedFile object, use the getFileNameFromUrl() method instead. This method also excepts the optional 2nd $unique parameter:, (*37)

$fileName = $mmm->getFileNameFromUrl($url, $unique);

Upload a file

$mmm->uploadFile($file);

Use uploadFile() to upload an UploadedFile file object. It will create all the necessary folders for this file's path within the upload_dir you set in your config.yml settings. Also, can accept a second optional unique parameter:, (*38)

$mmm->uploadFile($file, $unique);

Upload File from URL

$mmm->uploadFileFromUrl($url);

Same as uploadFile() but for URLs instead of UploadedFile objects. You will need allow_url_fopen enabled to use this. Also, can accept a second optional unique parameter:, (*39)

$mmm->uploadFileFromUrl($url, $unique);

Remove File

$mmm->removeFile($fileName);

Removes a file, and any empty subdirectories created for that file. Just pass in the filename., (*40)

Alternatively, if you want to remove a file, but keep any empty subdirectories created for it, use false as the second parameter to this method like so:, (*41)

$mmm->removeFile($fileName, false);

Get Web Path

$mmm->getWebPath($fileName);

Returns the web path of a file., (*42)

Twig

To get the web path to your file in a Twig template you can use the mass_media_web_path filter like so:, (*43)

<img src="{{ asset(product.photo|mass_media_web_path) }}">

The Versions

16/02 2017

dev-master

9999999-dev https://github.com/south634

File path management bundle for Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Steven McCarthy

file management