akaunting/app/Traits/Uploads.php

178 lines
4.6 KiB
PHP
Raw Permalink Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Traits;
2021-02-10 12:08:39 +03:00
use App\Models\Common\Media as MediaModel;
2021-05-08 18:18:00 +03:00
use App\Utilities\Date;
2021-10-05 19:14:39 +03:00
use Illuminate\Support\Str;
2021-04-16 00:59:43 +03:00
use MediaUploader;
2017-12-28 17:20:16 +03:00
2017-09-14 22:21:00 +03:00
trait Uploads
{
2018-01-02 19:00:07 +03:00
public function getMedia($file, $folder = 'settings', $company_id = null)
2017-12-28 17:20:16 +03:00
{
2018-01-02 19:00:07 +03:00
$path = '';
if (! $file || ! $file->isValid()) {
2018-01-02 19:00:07 +03:00
return $path;
}
2021-05-08 18:18:00 +03:00
$path = $this->getMediaFolder($folder, $company_id);
2017-12-28 17:20:16 +03:00
2021-10-05 19:14:39 +03:00
$file_name = $this->getMediaFileName($file);
2021-05-17 22:32:45 +03:00
return MediaUploader::makePrivate()
->beforeSave(function(MediaModel $media) {
$media->company_id = company_id();
2021-09-07 10:33:34 +03:00
$media->created_from = source_name();
$media->created_by = user_id();
2021-05-17 22:32:45 +03:00
})
->fromSource($file)
->toDirectory($path)
2021-10-05 19:14:39 +03:00
->useFilename($file_name)
2021-05-17 22:32:45 +03:00
->upload();
2017-12-28 17:20:16 +03:00
}
2019-03-25 16:11:03 +03:00
public function importMedia($file, $folder = 'settings', $company_id = null, $disk = null)
{
$path = '';
if (! $disk) {
2019-03-25 16:11:03 +03:00
$disk = config('mediable.default_disk');
}
2021-05-08 18:18:00 +03:00
$path = $this->getMediaFolder($folder, $company_id) . '/' . basename($file);
2019-03-25 16:11:03 +03:00
2021-05-17 22:32:45 +03:00
return MediaUploader::makePrivate()
->beforeSave(function(MediaModel $media) {
$media->company_id = company_id();
2021-09-07 10:33:34 +03:00
$media->created_from = source_name();
$media->created_by = user_id();
2021-05-17 22:32:45 +03:00
})
->importPath($disk, $path);
2019-03-25 16:11:03 +03:00
}
2021-02-10 12:08:39 +03:00
public function deleteMediaModel($model, $parameter, $request = null)
{
$medias = $model->$parameter;
2022-08-05 19:39:25 +03:00
if (! $medias) {
2021-02-10 12:08:39 +03:00
return;
}
$multiple = true;
if ($medias instanceof \App\Models\Common\Media) {
$multiple = false;
$medias = [$medias];
}
2021-02-10 12:08:39 +03:00
$already_uploaded = [];
if ($request && isset($request['uploaded_' . $parameter])) {
$uploaded = ($multiple) ? $request['uploaded_' . $parameter] : [$request['uploaded_' . $parameter]];
2021-02-10 12:08:39 +03:00
if (count($medias) == count($uploaded)) {
return;
}
foreach ($uploaded as $old_media) {
$already_uploaded[] = $old_media['id'];
}
}
2022-08-05 19:39:25 +03:00
foreach ((array) $medias as $media) {
2021-02-10 12:08:39 +03:00
if (in_array($media->id, $already_uploaded)) {
continue;
}
MediaModel::where('id', $media->id)->delete();
}
}
2021-05-08 18:18:00 +03:00
public function getMediaFolder($folder, $company_id = null)
{
2022-08-05 19:39:25 +03:00
if (! $company_id) {
2021-05-08 18:18:00 +03:00
$company_id = company_id();
}
$date = Date::now()->format('Y/m/d');
// 2021/04/09/34235/invoices
return $date . '/' . $company_id . '/' . $folder;
}
public function getMediaPathOnStorage($media)
{
2022-08-05 19:39:25 +03:00
if (! is_object($media)) {
2021-05-08 18:18:00 +03:00
return false;
}
2021-05-30 19:32:39 +03:00
$path = $media->getDiskPath();
2021-05-08 18:18:00 +03:00
if (! $media->fileExists()) {
2021-05-08 18:18:00 +03:00
return false;
}
2021-06-17 16:40:12 +03:00
return $path;
2021-05-08 18:18:00 +03:00
}
2021-05-11 23:43:19 +03:00
public function streamMedia($media, $disposition = 'attachment')
2021-05-11 23:43:19 +03:00
{
return response()->streamDownload(
function() use ($media) {
$stream = $media->stream();
2021-05-14 18:29:24 +03:00
while ($bytes = $stream->read(1024)) {
2021-05-11 23:43:19 +03:00
echo $bytes;
}
},
$media->basename,
[
2021-05-14 18:29:24 +03:00
'Content-Type' => $media->mime_type,
'Content-Length' => $media->size,
2021-05-11 23:43:19 +03:00
],
$disposition,
2021-05-11 23:43:19 +03:00
);
}
public function isLocalStorage()
{
return config('filesystems.disks.' . config('filesystems.default') . '.driver') == 'local';
}
2021-10-05 19:14:39 +03:00
public function getMediaFileName($file): string
{
$file_name = $this->filename($file);
if (Str::length($file_name) > '110') {
$file_name = Str::limit($file_name, 110);
}
return $file_name;
2021-10-05 19:14:39 +03:00
}
/**
* {@inheritdoc}
*/
public function filename($file): string
{
return pathinfo((string)$file->getClientOriginalName(), PATHINFO_FILENAME);
}
/**
* {@inheritdoc}
*/
public function extension($file): string
{
$extension = $file->getClientOriginalExtension();
if ($extension) {
return $extension;
}
return (string)$file->guessExtension();
}
2017-12-28 17:20:16 +03:00
}