Skip to content

Firefly III: a personal finances manager

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

HamuZ HamuZf696353e2c74
diff --git a/app/Console/Commands/CreateExport.php b/app/Console/Commands/CreateExport.php
index 73b4d3384f..4c66cceb91 100644
--- a/app/Console/Commands/CreateExport.php
+++ b/app/Console/Commands/CreateExport.php
@@ -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;
}
diff --git a/app/Console/Commands/EncryptFile.php b/app/Console/Commands/EncryptFile.php
index 08d9683afa..6c0e6d4272 100644
--- a/app/Console/Commands/EncryptFile.php
+++ b/app/Console/Commands/EncryptFile.php
@@ -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.
diff --git a/app/Export/ExpandedProcessor.php b/app/Export/ExpandedProcessor.php
index 4adb9499cf..ba5feceb21 100644
--- a/app/Export/ExpandedProcessor.php
+++ b/app/Export/ExpandedProcessor.php
@@ -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.
diff --git a/app/Export/Exporter/CsvExporter.php b/app/Export/Exporter/CsvExporter.php
index 6e4a7b96d1..f7d57c26b8 100644
--- a/app/Export/Exporter/CsvExporter.php
+++ b/app/Export/Exporter/CsvExporter.php
@@ -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, '');
- }
}
diff --git a/app/Helpers/Attachments/AttachmentHelper.php b/app/Helpers/Attachments/AttachmentHelper.php
index 09f22838f1..34a15813a5 100644
--- a/app/Helpers/Attachments/AttachmentHelper.php
+++ b/app/Helpers/Attachments/AttachmentHelper.php
@@ -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;
}
diff --git a/app/Repositories/Attachment/AttachmentRepository.php b/app/Repositories/Attachment/AttachmentRepository.php
index 501c846a0c..c3b9987639 100644
--- a/app/Repositories/Attachment/AttachmentRepository.php
+++ b/app/Repositories/Attachment/AttachmentRepository.php
@@ -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()));
}
diff --git a/app/Repositories/ExportJob/ExportJobRepository.php b/app/Repositories/ExportJob/ExportJobRepository.php
index 9ffd1b11b7..735e4e45f5 100644
--- a/app/Repositories/ExportJob/ExportJobRepository.php
+++ b/app/Repositories/ExportJob/ExportJobRepository.php
@@ -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 {
diff --git a/app/Services/Internal/File/EncryptService.php b/app/Services/Internal/File/EncryptService.php
index 5380efcd83..50558c8586 100644
--- a/app/Services/Internal/File/EncryptService.php
+++ b/app/Services/Internal/File/EncryptService.php
@@ -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);
}
}
diff --git a/tests/Unit/Helpers/Attachments/AttachmentHelperTest.php b/tests/Unit/Helpers/Attachments/AttachmentHelperTest.php
index 50483d0065..f6e3d86e47 100644
--- a/tests/Unit/Helpers/Attachments/AttachmentHelperTest.php
+++ b/tests/Unit/Helpers/Attachments/AttachmentHelperTest.php
@@ -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);
}