Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flexibility for cloud S3 upload path #15

Open
hasnatbabur opened this issue Aug 14, 2019 · 3 comments
Open

Flexibility for cloud S3 upload path #15

hasnatbabur opened this issue Aug 14, 2019 · 3 comments
Labels
enhancement New feature or request

Comments

@hasnatbabur
Copy link

hasnatbabur commented Aug 14, 2019

Your library structure is awesome. However the UploadMedia class did not find any flexibility on uploading files in custom (dynamic) S3 directory and also setting the file visibility.

Also if I have custom upload controller then how can I initiate $media object? please say.

I need to save images in 'images' root folder and videos in 'videos' root folder and all other files in 'files' folder. Also I should give the file visibility as public or private while saving to S3.

My Upload controller already detect the root path based on mime_type:

        !# UploadController.php
        // we need to detect path for the file type
        $mimeType = $request->file('file')->getMimeType();

        if(strpos($mimeType, 'image/') !== false){
            // image file
            $uploadPath = 'images';
        } elseif(strpos($mimeType, 'video/') !== false){
            // video file
            $uploadPath = 'videos';
        } else {
            // other file
            $uploadPath = 'files';
        }

        $filePath = $request->file('file')->storePublicly($uploadPath);
        $fileUrl = Storage::url($filePath);

Is it possible for your library to give such flexibility in near future?

Thanks.

@Jack97
Copy link
Contributor

Jack97 commented Aug 16, 2019

Definitely. I'll work on this as soon as possible.

@Jack97 Jack97 added the enhancement New feature or request label Aug 16, 2019
@rockymontana
Copy link

Would love to have this feature on local disk as well!
At the moment all files are dropped into the storage/app/public folder, but it would be awesome to be able to determine where on the filesystem they should be placed, or at least which subfolder to the public.
I'm doing a published/unpublished feature in our app so it'd be great to be able to push the files outside the publicly available space and then copy it to the public storage when desired (that's obviously a userland feature, but it might give a hint on why I'd need it).

@vishalinfyom
Copy link

vishalinfyom commented Jan 28, 2021

If you want to store your files on custom path here is the whole steps that you need to follow :

  1. Take new fields into media table called path (string)
 Schema::create('media', function (Blueprint $table) {
            $table->bigIncrements('id');
            ..............
            $table->string('path');
            $table->timestamps();
        });
  1. Create your own MediaUploader
<?php

namespace App\Media;


class MediaUploader extends \Optix\Media\MediaUploader
{
    public static $path;
    
    public static function updatePath($path)
    {
        self::$path = $path;
    }
    
    /**
     * Upload the file to the specified disk.
     *
     * @return mixed
     */
    public function upload()
    {
        $model = config('media.model');

        $media = new $model();

        $media->name = $this->name;
        $media->file_name = $this->fileName;
        $media->disk = $this->disk ?: config('media.disk');
        $media->mime_type = $this->file->getMimeType();
        $media->size = $this->file->getSize();
        $media->group = self::$path;

        $media->forceFill($this->attributes);

        $media->save();

        $media->filesystem()->putFileAs(
            $media->getDirectory(),
            $this->file,
            $this->fileName
        );

        return $media->fresh();
    }
}
  1. Create your own media model by extending their media :

<?php

namespace App\Models;

use Illuminate\Contracts\Filesystem\Filesystem;
use Optix\Media\HasMedia;
use Optix\Media\Models\Media as MediaModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class Media extends MediaModel
{
    public function getDirectory()
    {
        return $this->path. '/'.$this->id; // you can customize this however you want
    }
}
  1. Upload your media
    \App\Media\MediaUploader::updatePath('users/profile-photos');
    $media = \App\Media\MediaUploader::fromFile($file)->upload();
    $model->attachMedia($media, $mediaType);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants