fix local references in upload/export disk. first step for #1727.

This commit is contained in:
HamuZ HamuZ 2018-10-13 09:56:26 +03:00
parent cf11dfe73b
commit f696353e2c
9 changed files with 36 additions and 44 deletions

View File

@ -33,7 +33,7 @@ use FireflyIII\Repositories\ExportJob\ExportJobRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Console\Command;
use Storage;
use Illuminate\Support\Facades\Storage;
/**
* Class CreateExport.
@ -136,10 +136,14 @@ class CreateExport extends Command
$processor->createZipFile();
$disk = Storage::disk('export');
$fileName = sprintf('export-%s.zip', date('Y-m-d_H-i-s'));
$disk->move($job->key . '.zip', $fileName);
$localPath = storage_path('export') . '/' . $job->key . '.zip';
$this->line('The export has finished! You can find the ZIP file in this location:');
$this->line(storage_path(sprintf('export/%s', $fileName)));
// "move" from local to export disk
$disk->put($fileName, file_get_contents($localPath));
unlink($localPath);
$this->line('The export has finished! You can find the ZIP file in export disk with file name:');
$this->line($fileName);
return 0;
}

View File

@ -39,7 +39,7 @@ class EncryptFile extends Command
*
* @var string
*/
protected $description = 'Encrypts a file and places it in the storage/upload directory.';
protected $description = 'Encrypts a file and places it in the upload disk.';
/**
* The name and signature of the console command.

View File

@ -42,7 +42,7 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
use Storage;
use Illuminate\Support\Facades\Storage;
use ZipArchive;
/**
@ -184,7 +184,7 @@ class ExpandedProcessor implements ProcessorInterface
}
/**
* Create a ZIP file.
* Create a ZIP file locally (!) in storage_path('export').
*
* @return bool
*
@ -195,9 +195,9 @@ class ExpandedProcessor implements ProcessorInterface
{
$zip = new ZipArchive;
$file = $this->job->key . '.zip';
$fullPath = storage_path('export') . '/' . $file;
$localPath = storage_path('export') . '/' . $file;
if (true !== $zip->open($fullPath, ZipArchive::CREATE)) {
if (true !== $zip->open($localPath, ZipArchive::CREATE)) {
throw new FireflyException('Cannot store zip file.');
}
// for each file in the collection, add it to the zip file.

View File

@ -26,7 +26,7 @@ namespace FireflyIII\Export\Exporter;
use FireflyIII\Export\Entry\Entry;
use League\Csv\Writer;
use Storage;
use Illuminate\Support\Facades\Storage;
/**
* Class CsvExporter.
@ -57,15 +57,11 @@ class CsvExporter extends BasicExporter implements ExporterInterface
*/
public function run(): bool
{
// create temporary file:
$this->tempFile();
// necessary for CSV writer:
$fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->fileName;
// choose file name:
$this->fileName = $this->job->key . '-records.csv';
//we create the CSV into memory
$writer = Writer::createFromPath($fullPath);
$writer = Writer::createFromString('');
$rows = [];
// get field names for header row:
@ -86,18 +82,9 @@ class CsvExporter extends BasicExporter implements ExporterInterface
$rows[] = $line;
}
$writer->insertAll($rows);
$disk = Storage::disk('export');
$disk->put($this->fileName, $writer->getContent());
return true;
}
/**
* Make a temp file.
*/
private function tempFile()
{
$this->fileName = $this->job->key . '-records.csv';
// touch file in export directory:
$disk = Storage::disk('export');
$disk->put($this->fileName, '');
}
}

View File

@ -30,7 +30,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
use Log;
use Storage;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
@ -94,7 +94,7 @@ class AttachmentHelper implements AttachmentHelperInterface
}
/**
* Returns the file location for an attachment,
* Returns the file path relative to upload disk for an attachment,
*
* @param Attachment $attachment
*
@ -102,8 +102,7 @@ class AttachmentHelper implements AttachmentHelperInterface
*/
public function getAttachmentLocation(Attachment $attachment): string
{
$path = sprintf('%s%sat-%d.data', storage_path('upload'), DIRECTORY_SEPARATOR, (int)$attachment->id);
$path = sprintf('%sat-%d.data', DIRECTORY_SEPARATOR, (int)$attachment->id);
return $path;
}

View File

@ -34,7 +34,7 @@ use FireflyIII\User;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Collection;
use Log;
use Storage;
use Illuminate\Support\Facades\Storage;
/**
* Class AttachmentRepository.
@ -66,9 +66,9 @@ class AttachmentRepository implements AttachmentRepositoryInterface
/** @var AttachmentHelperInterface $helper */
$helper = app(AttachmentHelperInterface::class);
$file = $helper->getAttachmentLocation($attachment);
$path = $helper->getAttachmentLocation($attachment);
try {
unlink($file);
Storage::disk('upload')->delete($path);
} catch (Exception $e) {
Log::error(sprintf('Could not delete file for attachment %d: %s', $attachment->id, $e->getMessage()));
}

View File

@ -28,8 +28,8 @@ use FireflyIII\Models\ExportJob;
use FireflyIII\User;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage;
use Log;
use Storage;
/**
* Class ExportJobRepository.
@ -74,15 +74,17 @@ class ExportJobRepository implements ExportJobRepositoryInterface
->whereIn('status', ['never_started', 'export_status_finished', 'export_downloaded'])
->get();
$disk = Storage::disk('export');
$files = $disk->files();
// loop set:
/** @var ExportJob $entry */
foreach ($set as $entry) {
$key = $entry->key;
$files = scandir(storage_path('export'), SCANDIR_SORT_NONE);
/** @var string $file */
/** @var array $file */
foreach ($files as $file) {
if (0 === strpos($file, $key)) {
unlink(storage_path('export') . DIRECTORY_SEPARATOR . $file);
if (0 === strpos($file['basename'], $key)) {
$disk->delete($file['path']);
}
}
try {

View File

@ -26,6 +26,7 @@ namespace FireflyIII\Services\Internal\File;
use Crypt;
use FireflyIII\Exceptions\FireflyException;
use Illuminate\Contracts\Encryption\EncryptException;
use Illuminate\Support\Facades\Storage;
use Log;
/**
@ -63,9 +64,8 @@ class EncryptService
throw new FireflyException($message);
}
$newName = sprintf('%s.upload', $key);
$path = storage_path('upload') . '/' . $newName;
file_put_contents($path, $content);
$disk = Storage::disk('upload');
$disk->put($newName, $content);
}
}

View File

@ -56,7 +56,7 @@ class AttachmentHelperTest extends TestCase
{
$attachment = Attachment::first();
$helper = new AttachmentHelper;
$path = $path = sprintf('%s%sat-%d.data', storage_path('upload'), DIRECTORY_SEPARATOR, (int)$attachment->id);
$path = $path = sprintf('%sat-%d.data', DIRECTORY_SEPARATOR, (int)$attachment->id);
$this->assertEquals($helper->getAttachmentLocation($attachment), $path);
}