mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-01-16 19:05:08 -05:00
105 lines
3.2 KiB
PHP
105 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\StudioPlus\Repositories;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use Modules\StudioPlus\Entities\ContentWatermark;
|
|
use Modules\StudioPlus\Transformers\ContentWatermarkDatabaseResource;
|
|
use Modules\StudioPlus\Transformers\ContentWatermarkRequestResource;
|
|
|
|
class ContentWatermarksRepository
|
|
{
|
|
public function getAll()
|
|
{
|
|
return ContentWatermark::with(['contentType', 'contentVariant'])->get();
|
|
}
|
|
|
|
public function getActive()
|
|
{
|
|
return ContentWatermark::with(['contentType', 'contentVariant'])->where('active', true)->get();
|
|
}
|
|
|
|
public function getCollection()
|
|
{
|
|
$watermarks = $this->getAll();
|
|
|
|
return ContentWatermarkDatabaseResource::collection($watermarks);
|
|
}
|
|
|
|
public function find($id)
|
|
{
|
|
return ContentWatermark::find($id);
|
|
}
|
|
|
|
public function store($data)
|
|
{
|
|
if ($data instanceof Request) {
|
|
if ($data->type['value'] == 'image' && $data->has('file')) {
|
|
$file = $data->file;
|
|
}
|
|
|
|
$data = ContentWatermarkRequestResource::make($data)->toArray($data);
|
|
}
|
|
|
|
$watermark = ContentWatermark::create($data);
|
|
|
|
if (isset($file) && $file) {
|
|
$filename = $this->getSafeFilename($file);
|
|
$local_storage_path = $this->getLocalStoragePath($watermark->id);
|
|
Storage::put("$local_storage_path/filename.dat", $filename);
|
|
$watermark_local_path = Storage::putFileAs($local_storage_path, $file, $filename);
|
|
$watermark->watermark = "app/$watermark_local_path";
|
|
$watermark->update();
|
|
}
|
|
|
|
return $watermark;
|
|
}
|
|
|
|
public function update($watermark, $data)
|
|
{
|
|
if ($data instanceof Request) {
|
|
if ($data->type['value'] == 'image' && $data->has('file')) {
|
|
$file = $data->file;
|
|
}
|
|
|
|
$data = ContentWatermarkRequestResource::make($data)->toArray($data);
|
|
}
|
|
|
|
$watermark->fill($data)->update();
|
|
|
|
if (isset($file) && $file) {
|
|
$filename = $this->getSafeFilename($file);
|
|
$local_storage_path = $this->getLocalStoragePath($watermark->id);
|
|
Storage::deleteDirectory($local_storage_path);
|
|
Storage::put("$local_storage_path/filename.dat", $filename);
|
|
$watermark_local_path = Storage::putFileAs($local_storage_path, $file, $filename);
|
|
$watermark->watermark = "app/$watermark_local_path";
|
|
$watermark->update();
|
|
}
|
|
|
|
return $watermark->fresh();
|
|
}
|
|
|
|
public function toResource($watermark)
|
|
{
|
|
return ContentWatermarkDatabaseResource::make($watermark);
|
|
}
|
|
|
|
protected function getLocalStoragePath($id)
|
|
{
|
|
return "CreatorStudio/watermarks/$id";
|
|
}
|
|
|
|
protected function getSafeFilename($file)
|
|
{
|
|
$stamp = Carbon::now()->format('YmdHis');
|
|
$uid = $stamp . Str::random();
|
|
$ext = pathinfo($file->getClientOriginalName())['extension'];
|
|
|
|
return "$uid.$ext";
|
|
}
|
|
}
|