fipamo/app/Services/Upkeep/MaintenanceService.php

163 lines
6.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Services\Upkeep;
use App\Services\Data\SettingsService;
use Carbon\Carbon;
class MaintenanceService
{
protected $settings;
protected $filePaths = [];
public function __construct(SettingsService $settingsService)
{
$this->settings = $settingsService;
$this->filePaths = ['../public/assets/images/blog',
'../public/assets/images/user', '../public/assets/docs/blog',
'../public/assets/video/blog', '../public/assets/sound/blog'];
}
public function createContentBackUp()
{
//make sure back directory is there
$stamp = Carbon::now()->format("YmdHis");
if (!is_dir(env('FIPAMO_BACKUPS'))) {
mkdir(env('FIPAMO_BACKUPS'), 0755, true);
}
//creat backup zip
$zip = new \ZipArchive();
$zip->open(
env('FIPAMO_BACKUPS') . '/backup-content-' . $stamp . '.zip',
\ZipArchive::CREATE | \ZipArchive::OVERWRITE
);
//gather data and path info for md pages
$pagePath = env('PAGES_PATH');
$yearPaths = glob($pagePath . '/*', GLOB_ONLYDIR);
foreach ($yearPaths as $years) {
$year = explode('/', $years);
//grap the index and save it
if (trim($year[3]) == 'start') {
$options = [
'add_path' => 'content/pages/' . $year[3] . '/',
'remove_all_path' => true,
];
$zip->addGlob($years . '/*.md', GLOB_BRACE, $options);
}
$monthsPath = glob($pagePath . '/' . $year[3] . '/*', GLOB_ONLYDIR);
foreach ($monthsPath as $months) {
$month = explode('/', $months);
//once info is collected, add md pages to zip
$options = [
'add_path' => 'content/pages/' . $year[3] . '/' . $month[4] . '/',
'remove_all_path' => true,
];
$zip->addGlob($months . '/*.md', GLOB_BRACE, $options);
}
}
//gather paths for blog images
$blogImages = [];
$dir = new \RecursiveDirectoryIterator('../public/assets/images/blog');
$flat = new \RecursiveIteratorIterator($dir);
$files = new \RegexIterator($flat, '/\.png|jpg|gif$/i');
foreach ($files as $file) {
$blogImages[] = ['path' => $file->getPath(), 'file' => $file->getFilename()];
};
//gather paths for user images
$userImages = [];
if (is_dir('../public/assets/images/user')) {
$dir = new \RecursiveDirectoryIterator('../public/assets/images/user');
$flat = new \RecursiveIteratorIterator($dir);
$files = new \RegexIterator($flat, '/\.png|jpg|gif$/i');
foreach ($files as $file) {
$userImages[] = ['path' => $file->getPath(), 'file' => $file->getFilename()];
};
}
//gather paths for blog documents
$blogDocs = [];
if (is_dir('../public/assets/docs/blog')) {
$dir = new \RecursiveDirectoryIterator('../public/assets/docs/blog');
$flat = new \RecursiveIteratorIterator($dir);
$files = new \RegexIterator($flat, '/\.txt|pdf|rtf$/i');
foreach ($files as $file) {
$blogDocs[] = ['path' => $file->getPath(), 'file' => $file->getFilename()];
};
}
//gather paths for blog videos
$blogVids = [];
if (is_dir('../public/assets/video/blog')) {
$dir = new \RecursiveDirectoryIterator('../public/assets/video/blog');
$flat = new \RecursiveIteratorIterator($dir);
$files = new \RegexIterator($flat, '/\.mp4$/i');
foreach ($files as $file) {
$blogVids[] = ['path' => $file->getPath(), 'file' => $file->getFilename()];
};
}
//add directory for settings and save them
$zip->addEmptyDir('config');
$zip->addEmptyDir('assets');
$zip->addFile(env('SETTINGS_PATH'), 'config/settings.json');
$zip->addFile(env('FOLKS_PATH'), 'config/folks.json');
$zip->addFile(env('TAGS_PATH'), 'config/tags.json');
// create temp files for image lists
file_put_contents(env('FIPAMO_BACKUPS') . '/blog_images_temp.json', json_encode($blogImages));
file_put_contents(env('FIPAMO_BACKUPS') . '/user_images_temp.json', json_encode($userImages));
file_put_contents(env('FIPAMO_BACKUPS') . '/blog_docs_temp.json', json_encode($blogDocs));
file_put_contents(env('FIPAMO_BACKUPS') . '/blog_vids_temp.json', json_encode($blogVids));
//add to zip
$zip->addFile(env('FIPAMO_BACKUPS') . '/blog_images_temp.json', 'assets/blog_images.json');
$zip->addFile(env('FIPAMO_BACKUPS') . '/user_images_temp.json', 'assets/user_images.json');
$zip->addFile(env('FIPAMO_BACKUPS') . '/blog_docs_temp.json', 'assets/blog_docs.json');
$zip->addFile(env('FIPAMO_BACKUPS') . '/blog_vids_temp.json', 'assets/blog_videos.json');
//save zip file
$zip->close();
//clean up temp files
unlink(env('FIPAMO_BACKUPS') . '/blog_images_temp.json');
unlink(env('FIPAMO_BACKUPS') . '/user_images_temp.json');
unlink(env('FIPAMO_BACKUPS') . '/blog_docs_temp.json');
unlink(env('FIPAMO_BACKUPS') . '/blog_vids_temp.json');
//update settings file with latest back up date
$this->settings->updateGlobalData('last_content_backup', $stamp);
return ['message' => "Content backup created. THIS IS A SAFE SPACE!"];
}
public function createFileBackUp()
{
$stamp = Carbon::now()->format("YmdHis");
$zip = new \ZipArchive();
$zip->open(
env('FIPAMO_BACKUPS') . '/backup-files-' . $stamp . '.zip',
\ZipArchive::CREATE | \ZipArchive::OVERWRITE
);
//gather data and path info for blog images
foreach ($this->filePaths as $path) {
$yearPaths = glob($path . '/*', GLOB_ONLYDIR);
foreach ($yearPaths as $years) {
$year = explode('/', $years);
$monthsPath = glob($path . '/' . $year[5] . '/*', GLOB_ONLYDIR);
foreach ($monthsPath as $months) {
$month = explode('/', $months);
//once info is collected, add images pages to zip
$options = [
'add_path' => substr($path, 3) . '/' . $year[5] . '/' . $month[6] . '/',
'remove_all_path' => true,
];
$zip->addGlob($months . '/*.*', GLOB_BRACE, $options);
}
}
}
$zip->close();
$this->settings->updateGlobalData('last_files_backup', $stamp);
return ['message' => "Files are backed up. Breath Easy!"];
}
}