Backups endpoint

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2022-06-28 22:56:18 -07:00
parent bb7662a214
commit 1debdc47cf
3 changed files with 111 additions and 0 deletions

View file

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api;
use App\Http\Transformers\BackupsTransformer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Ldap;
@ -265,4 +266,45 @@ class SettingsController extends Controller
return (new LoginAttemptsTransformer)->transformLoginAttempts($login_attempt_results, $total);
}
public function listBackups() {
$settings = Setting::getSettings();
$path = 'app/backups';
$backup_files = Storage::files($path);
$files_raw = [];
$count = 0;
if (count($backup_files) > 0) {
for ($f = 0; $f < count($backup_files); $f++) {
// Skip dotfiles like .gitignore and .DS_STORE
if ((substr(basename($backup_files[$f]), 0, 1) != '.')) {
$file_timestamp = Storage::lastModified($backup_files[$f]);
$files_raw[] = [
'filename' => basename($backup_files[$f]),
'filesize' => Setting::fileSizeConvert(Storage::size($backup_files[$f])),
'modified_value' => $file_timestamp,
'modified_display' => date($settings->date_display_format.' '.$settings->time_display_format, $file_timestamp),
];
$count++;
}
}
}
$files = array_reverse($files_raw);
return (new BackupsTransformer())->transformBackups($files, $count);
}
public function downloadBackup($file) {
$path = '';
return response()->download($path, $file->name, $headers);
}
}

View file

@ -0,0 +1,55 @@
<?php
namespace App\Http\Transformers;
use App\Helpers\Helper;
use Gate;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Storage;
class BackupsTransformer
{
public function transformBackups(Array $files, $total)
{
$array = [];
foreach ($files as $file) {
$array[] = self::transformCategory($file);
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
public function transformCategory(Array $file = null)
{
if ($file) {
$array = [
'id' => (int) $file->id,
'name' => e($category->name),
'image' => ($category->image) ? Storage::disk('public')->url('categories/'.e($category->image)) : null,
'category_type' => ucwords(e($category->category_type)),
'has_eula' => ($category->getEula() ? true : false),
'use_default_eula' => ($category->use_default_eula=='1' ? true : false),
'eula' => ($category->getEula()),
'checkin_email' => ($category->checkin_email == '1'),
'require_acceptance' => ($category->require_acceptance == '1'),
'item_count' => (int) $category->itemCount(),
'assets_count' => (int) $category->assets_count,
'accessories_count' => (int) $category->accessories_count,
'consumables_count' => (int) $category->consumables_count,
'components_count' => (int) $category->components_count,
'licenses_count' => (int) $category->licenses_count,
'created_at' => Helper::getFormattedDateObject($category->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($category->updated_at, 'datetime'),
];
$permissions_array['available_actions'] = [
'update' => Gate::allows('update', Category::class),
'delete' => $category->isDeletable(),
];
$array += $permissions_array;
return $array;
}
}
}

View file

@ -786,6 +786,20 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'throttle:api']], functi
]
)->name('api.settings.mailtest');
Route::get('backups',
[
Api\SettingsController::class,
'listBackups'
]
)->name('api.settings.backups.index');
Route::get('backups/download/{file}',
[
Api\SettingsController::class,
'downloadBackup'
]
)->name('api.settings.backups.download');
});
Route::resource('settings',