Refactor some code to handle command line imports.

This commit is contained in:
James Cole
2018-05-12 19:09:34 +02:00
parent 07da2fdda3
commit 9c507f7f62
10 changed files with 248 additions and 85 deletions

View File

@@ -475,6 +475,59 @@ class ImportJobRepository implements ImportJobRepositoryInterface
$this->user = $user;
}
/**
* Handle upload for job.
*
* @param ImportJob $job
* @param string $name
* @param UploadedFile $file
*
* @return MessageBag
* @throws FireflyException
*/
public function storeCLIUpload(ImportJob $job, string $name, string $fileName): MessageBag
{
$messages = new MessageBag;
if (!file_exists($fileName)) {
$messages->add('notfound', sprintf('File not found: %s', $fileName));
return $messages;
}
$count = $job->attachments()->get()->filter(
function (Attachment $att) use ($name) {
return $att->filename === $name;
}
)->count();
if ($count > 0) {
// don't upload, but also don't complain about it.
Log::error(sprintf('Detected duplicate upload. Will ignore second "%s" file.', $name));
return new MessageBag;
}
$content = file_get_contents($fileName);
$attachment = new Attachment; // create Attachment object.
$attachment->user()->associate($job->user);
$attachment->attachable()->associate($job);
$attachment->md5 = md5($content);
$attachment->filename = $name;
$attachment->mime = 'plain/txt';
$attachment->size = \strlen($content);
$attachment->uploaded = 0;
$attachment->save();
$encrypted = Crypt::encrypt($content);
// store it:
$this->uploadDisk->put($attachment->fileName(), $encrypted);
$attachment->uploaded = 1; // update attachment
$attachment->save();
// return it.
return new MessageBag;
}
/**
* Handle upload for job.
*

View File

@@ -56,6 +56,17 @@ interface ImportJobRepositoryInterface
*/
public function storeFileUpload(ImportJob $job, string $name, UploadedFile $file): MessageBag;
/**
* Store file.
*
* @param ImportJob $job
* @param string $name
* @param string $fileName
*
* @return MessageBag
*/
public function storeCLIUpload(ImportJob $job, string $name, string $fileName): MessageBag;
/**
* @param ImportJob $job
* @param array $transactions