laravel-media
laravel-media copied to clipboard
Flexibility for cloud S3 upload path
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.
Definitely. I'll work on this as soon as possible.
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).
If you want to store your files on custom path here is the whole steps that you need to follow :
- Take new fields into media table called path (string)
Schema::create('media', function (Blueprint $table) {
$table->bigIncrements('id');
..............
$table->string('path');
$table->timestamps();
});
- 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();
}
}
- 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
}
}
- Upload your media
\App\Media\MediaUploader::updatePath('users/profile-photos');
$media = \App\Media\MediaUploader::fromFile($file)->upload();
$model->attachMedia($media, $mediaType);