2016-02-04 10:16:16 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ExportJobRepository.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-02-04 10:16:16 -06:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-04 10:16:16 -06:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-05-20 05:41:23 -05:00
|
|
|
|
2016-02-04 10:16:16 -06:00
|
|
|
namespace FireflyIII\Repositories\ExportJob;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Models\ExportJob;
|
2016-03-03 01:53:05 -06:00
|
|
|
use FireflyIII\User;
|
2016-02-04 10:16:16 -06:00
|
|
|
use Illuminate\Support\Str;
|
2017-09-15 00:00:53 -05:00
|
|
|
use Log;
|
2017-10-05 04:49:06 -05:00
|
|
|
use Storage;
|
2016-02-04 10:16:16 -06:00
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class ExportJobRepository.
|
2016-02-04 10:16:16 -06:00
|
|
|
*/
|
|
|
|
class ExportJobRepository implements ExportJobRepositoryInterface
|
|
|
|
{
|
2016-03-03 01:53:05 -06:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
2016-12-27 08:46:52 -06:00
|
|
|
/**
|
|
|
|
* @param ExportJob $job
|
|
|
|
* @param string $status
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function changeStatus(ExportJob $job, string $status): bool
|
|
|
|
{
|
2017-09-15 00:00:53 -05:00
|
|
|
Log::debug(sprintf('Change status of job #%d to "%s"', $job->id, $status));
|
2016-12-27 08:46:52 -06:00
|
|
|
$job->change($status);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-04 10:16:16 -06:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function cleanup(): bool
|
2016-02-04 10:16:16 -06:00
|
|
|
{
|
|
|
|
$dayAgo = Carbon::create()->subDay();
|
2016-02-04 10:50:35 -06:00
|
|
|
$set = ExportJob::where('created_at', '<', $dayAgo->format('Y-m-d H:i:s'))
|
|
|
|
->whereIn('status', ['never_started', 'export_status_finished', 'export_downloaded'])
|
|
|
|
->get();
|
|
|
|
|
|
|
|
// loop set:
|
|
|
|
/** @var ExportJob $entry */
|
|
|
|
foreach ($set as $entry) {
|
|
|
|
$key = $entry->key;
|
|
|
|
$len = strlen($key);
|
|
|
|
$files = scandir(storage_path('export'));
|
|
|
|
/** @var string $file */
|
|
|
|
foreach ($files as $file) {
|
|
|
|
if (substr($file, 0, $len) === $key) {
|
|
|
|
unlink(storage_path('export') . DIRECTORY_SEPARATOR . $file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$entry->delete();
|
|
|
|
}
|
2016-02-04 10:16:16 -06:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ExportJob
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function create(): ExportJob
|
2016-02-04 10:16:16 -06:00
|
|
|
{
|
2016-05-02 13:49:19 -05:00
|
|
|
$count = 0;
|
|
|
|
while ($count < 30) {
|
|
|
|
$key = Str::random(12);
|
|
|
|
$existing = $this->findByKey($key);
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $existing->id) {
|
2016-05-02 13:49:19 -05:00
|
|
|
$exportJob = new ExportJob;
|
|
|
|
$exportJob->user()->associate($this->user);
|
|
|
|
$exportJob->key = Str::random(12);
|
|
|
|
$exportJob->status = 'export_status_never_started';
|
|
|
|
$exportJob->save();
|
2016-05-15 11:36:40 -05:00
|
|
|
|
2016-05-02 13:49:19 -05:00
|
|
|
// breaks the loop:
|
|
|
|
|
|
|
|
return $exportJob;
|
|
|
|
}
|
2017-11-15 05:25:49 -06:00
|
|
|
++$count;
|
2016-05-02 13:49:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return new ExportJob;
|
2016-02-04 10:16:16 -06:00
|
|
|
}
|
|
|
|
|
2016-12-25 05:55:22 -06:00
|
|
|
/**
|
|
|
|
* @param ExportJob $job
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function exists(ExportJob $job): bool
|
|
|
|
{
|
|
|
|
$disk = Storage::disk('export');
|
|
|
|
$file = $job->key . '.zip';
|
|
|
|
|
|
|
|
return $disk->exists($file);
|
|
|
|
}
|
|
|
|
|
2016-02-04 10:16:16 -06:00
|
|
|
/**
|
2016-02-05 02:25:15 -06:00
|
|
|
* @param string $key
|
2016-02-04 10:16:16 -06:00
|
|
|
*
|
2017-01-02 02:23:24 -06:00
|
|
|
* @return ExportJob
|
2016-02-04 10:16:16 -06:00
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function findByKey(string $key): ExportJob
|
2016-02-04 10:16:16 -06:00
|
|
|
{
|
2016-08-11 03:21:32 -05:00
|
|
|
$result = $this->user->exportJobs()->where('key', $key)->first(['export_jobs.*']);
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $result) {
|
2016-05-02 13:49:19 -05:00
|
|
|
return new ExportJob;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2016-02-04 10:16:16 -06:00
|
|
|
}
|
|
|
|
|
2016-12-25 05:55:22 -06:00
|
|
|
/**
|
|
|
|
* @param ExportJob $job
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getContent(ExportJob $job): string
|
|
|
|
{
|
|
|
|
$disk = Storage::disk('export');
|
|
|
|
$file = $job->key . '.zip';
|
|
|
|
$content = $disk->get($file);
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
2017-01-30 09:46:30 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
2016-02-10 09:01:18 -06:00
|
|
|
}
|