305 lines
8.7 KiB
PHP
305 lines
8.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Backend;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use App\Http\Models\FileImageModel;
|
|
use App\Http\Models\FolderModel;
|
|
use DateTime;
|
|
use Redirect;
|
|
use View;
|
|
use Auth;
|
|
use Validator;
|
|
use ZipArchive;
|
|
|
|
class ImageController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$folder = FolderModel::where('folder_type', 3)
|
|
->orderBy('id', 'DESC')
|
|
->get();
|
|
|
|
$data = FileImageModel::orderBy('id', 'DESC')
|
|
->where('folder_id', '=', null)
|
|
->orderBy('id', 'DESC')
|
|
->paginate(100);
|
|
|
|
$urlsImage = $data->map(function ($fileObj) {
|
|
return [
|
|
'id' => $fileObj->id,
|
|
'url' => env('R2_SCHEMA_URL') . $fileObj->name,
|
|
'name' => $fileObj->name,
|
|
'created_at' => $fileObj->created_at,
|
|
];
|
|
});
|
|
|
|
return View::make("backend/image/index")
|
|
->with('folderView', $folder)
|
|
->with('itemView', $urlsImage);
|
|
}
|
|
|
|
public function folder($folderId)
|
|
{
|
|
$folder = FolderModel::where('id', $folderId)->first();
|
|
$data = FileImageModel::where('folder_id', $folderId)
|
|
->orderBy('id', 'DESC')
|
|
->paginate(100);
|
|
|
|
$urls = $data->map(function ($fileObj) {
|
|
return [
|
|
'id' => $fileObj->id,
|
|
'url' => env('R2_SCHEMA_URL') . $fileObj->name,
|
|
'name' => $fileObj->name,
|
|
'created_at' => $fileObj->created_at,
|
|
];
|
|
});
|
|
|
|
return View::make("backend/image/folder/index")
|
|
->with('folderView', $folder)
|
|
->with('itemView', $urls);
|
|
}
|
|
|
|
public function add($folderId = null)
|
|
{
|
|
return View::make("backend/image/add")
|
|
->with('folderIdView', $folderId);
|
|
}
|
|
|
|
public function insert(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'fileName' => 'required|string'
|
|
]);
|
|
|
|
// prepared data
|
|
$folderId = $request->input('folderId');
|
|
$data = [
|
|
'folder_id' => null,
|
|
'user_id' => Auth::user()->id,
|
|
'name' => $validated['fileName'],
|
|
'created_at' => new DateTime(),
|
|
];
|
|
// If $folderId is not null, add it to the $data array
|
|
if ($folderId !== null) {
|
|
$data['folder_id'] = $folderId;
|
|
}
|
|
|
|
$id = FileImageModel::insertGetId($data);
|
|
|
|
// insert to database
|
|
if ($id > 0) { // insert success then return ID
|
|
return response()->json(['message' => 'File name saved successfully'], 200);
|
|
} else { // insert fail
|
|
return response()->json(['message' => 'Failed to save file name'], 500);
|
|
}
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
// get by id
|
|
$data = FileImageModel::where('id', $id)->first();
|
|
|
|
if (is_null($data)) { // not found
|
|
return Redirect::to("image")
|
|
->withInput()
|
|
->with("messageFail", "Fail")
|
|
->with("messageDetail", 'Not found data');
|
|
}
|
|
|
|
return View::make('backend/image/edit')
|
|
->with('idView', $id)
|
|
->with('itemView', $data);
|
|
}
|
|
|
|
public function view($id)
|
|
{
|
|
// get by id
|
|
$data = FileImageModel::where('id', $id)->first();
|
|
|
|
if (is_null($data)) { // not found
|
|
return Redirect::to("image")
|
|
->withInput()
|
|
->with("messageFail", "Fail")
|
|
->with("messageDetail", 'Not found data');
|
|
}
|
|
|
|
return View::make('backend/image/view')
|
|
->with('idView', $id)
|
|
->with('itemView', $data);
|
|
}
|
|
|
|
public function update(Request $request)
|
|
{
|
|
// keep data en
|
|
$id = $request->input('id');
|
|
|
|
// redirect with message
|
|
return Redirect::to("image")
|
|
->withInput()
|
|
->with("messageSuccess", "Success")
|
|
->with("messageDetail", 'Updated success');
|
|
}
|
|
|
|
public function delete(Request $request)
|
|
{
|
|
$id = $request->input('item_id');
|
|
FileImageModel::where('id', '=', $id)->delete();
|
|
return response()->json([
|
|
'status' => true,
|
|
'message' => "Deleted success"
|
|
]);
|
|
}
|
|
|
|
|
|
// Folder
|
|
|
|
public function addFolder()
|
|
{
|
|
return View::make("backend/image/folder/add");
|
|
}
|
|
|
|
public function insertFolder(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string'
|
|
]);
|
|
|
|
// exist folder name
|
|
$exist = FolderModel::where('name', $validated['name'])
|
|
->where('folder_type', 3)
|
|
->first();
|
|
|
|
if ($exist != null) {
|
|
return Redirect::to("image")
|
|
->withInput()
|
|
->with("messageFail", "Fail")
|
|
->with("messageDetail", 'Folder already exist');
|
|
}
|
|
|
|
|
|
// prepared data
|
|
$data = [
|
|
'name' => $validated['name'],
|
|
'folder_type' => 3,
|
|
'created_at' => new DateTime(),
|
|
];
|
|
|
|
$id = FolderModel::insertGetId($data);
|
|
|
|
// insert to database
|
|
if ($id > 0) { // insert success then return ID
|
|
return Redirect::to("image")
|
|
->withInput()
|
|
->with("messageSuccess", "Success")
|
|
->with("messageDetail", 'Create folder success');
|
|
} else { // insert fail
|
|
return Redirect::to("image")
|
|
->withInput()
|
|
->with("messageSuccess", "Fail")
|
|
->with("messageDetail", 'Create folder unsuccess');
|
|
}
|
|
}
|
|
|
|
public function editFolder($id)
|
|
{
|
|
// get by id
|
|
$data = FolderModel::where('id', $id)
|
|
->first();
|
|
if (is_null($data)) { // not found
|
|
return Redirect::to("image/folder/" . $id)
|
|
->withInput()
|
|
->with("messageFail", "Fail")
|
|
->with("messageDetail", 'Not found data');
|
|
}
|
|
|
|
return View::make('backend/image/folder/edit')
|
|
->with('idView', $id)
|
|
->with('itemView', $data);
|
|
}
|
|
|
|
public function updateFolder(Request $request)
|
|
{
|
|
// keep data en
|
|
$id = $request->input('id');
|
|
$name = $request->input('name');
|
|
|
|
// validate
|
|
$validator = Validator::make($request->all(), [
|
|
'name' => 'required'
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return Redirect::to("image/folder/" . $id)
|
|
->withInput()
|
|
->with("messageFail", "Fail")
|
|
->with("messageDetail", 'Data can not be empty');
|
|
}
|
|
|
|
// check existing
|
|
$exist = FolderModel::where('id', '!=', $id)
|
|
->where('folder_type', 3)
|
|
->where('name', $name)
|
|
->count();
|
|
|
|
if (
|
|
$exist > 0
|
|
) {
|
|
return Redirect::to("image/edit-folder/" . $id)
|
|
->withInput()
|
|
->with("messageFail", "Fail")
|
|
->with("messageDetail", 'Name already exist');
|
|
}
|
|
|
|
$data = [
|
|
'name' => $name,
|
|
];
|
|
|
|
FolderModel::where('id', $id)->update($data);
|
|
|
|
return Redirect::to("image")
|
|
->with("messageSuccess", "Success")
|
|
->with("messageDetail", 'Updated success');
|
|
}
|
|
|
|
public function deleteFolder(Request $request)
|
|
{
|
|
$id = $request->input('item_id');
|
|
$hasFilesInFolder = FileImageModel::where('folder_id', $id)->count();
|
|
if ($hasFilesInFolder > 0) { // not found
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => "Can not delete already in folder"
|
|
]);
|
|
} else {
|
|
// delete user in database
|
|
FolderModel::where('id', $id)->delete();
|
|
return response()->json([
|
|
'status' => true,
|
|
'message' => "Delete successfully"
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function downloadFolder($folderId)
|
|
{
|
|
$files = FileImageModel::where('folder_id', $folderId)->get();
|
|
$zip = new ZipArchive;
|
|
$folder = FolderModel::where('id', $folderId)->first();
|
|
$zipFileName = $folder->name . '.zip';
|
|
$zipPath = storage_path('app/' . $zipFileName);
|
|
|
|
if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) {
|
|
foreach ($files as $file) {
|
|
$filePath = env('R2_SCHEMA_URL') . $file->name;
|
|
$fileContents = file_get_contents($filePath); // Download each file from R2
|
|
$zip->addFromString($file->name, $fileContents);
|
|
}
|
|
$zip->close();
|
|
}
|
|
|
|
return response()->download($zipPath)->deleteFileAfterSend(true);
|
|
}
|
|
}
|