Skip to content

Firefly III: a personal finances manager

Basic attachment download function.

James Colecc1af60cb419
diff --git a/app/Helpers/Attachments/AttachmentHelper.php b/app/Helpers/Attachments/AttachmentHelper.php
index 8390316bce..7b98a98f4a 100644
--- a/app/Helpers/Attachments/AttachmentHelper.php
+++ b/app/Helpers/Attachments/AttachmentHelper.php
@@ -20,7 +20,7 @@ class AttachmentHelper implements AttachmentHelperInterface
// move to config:
protected $maxUploadSize = 1048576; // 1MB per file
- protected $allowedMimes = ['image/png','image/jpeg','application/pdf'];
+ protected $allowedMimes = ['image/png', 'image/jpeg', 'application/pdf'];
public $errors;
public $messages;
@@ -34,6 +34,18 @@ class AttachmentHelper implements AttachmentHelperInterface
$this->messages = new MessageBag;
}
+ /**
+ * @param Attachment $attachment
+ *
+ * @return mixed
+ */
+ public function getAttachmentLocation(Attachment $attachment)
+ {
+ $path = storage_path('upload') . DIRECTORY_SEPARATOR . 'at-' . $attachment->id . '.data';
+
+ return $path;
+ }
+
/**
* @param Model $model
*
diff --git a/app/Helpers/Attachments/AttachmentHelperInterface.php b/app/Helpers/Attachments/AttachmentHelperInterface.php
index a1e0bfb7db..6c07e64a9e 100644
--- a/app/Helpers/Attachments/AttachmentHelperInterface.php
+++ b/app/Helpers/Attachments/AttachmentHelperInterface.php
@@ -2,6 +2,7 @@
namespace FireflyIII\Helpers\Attachments;
+use FireflyIII\Models\Attachment;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\MessageBag;
@@ -30,4 +31,11 @@ interface AttachmentHelperInterface
*/
public function getMessages();
+ /**
+ * @param Attachment $attachment
+ *
+ * @return mixed
+ */
+ public function getAttachmentLocation(Attachment $attachment);
+
}
\ No newline at end of file
diff --git a/app/Http/Controllers/AttachmentController.php b/app/Http/Controllers/AttachmentController.php
new file mode 100644
index 0000000000..8ff0106b3d
--- /dev/null
+++ b/app/Http/Controllers/AttachmentController.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace FireflyIII\Http\Controllers;
+
+
+use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
+use FireflyIII\Models\Attachment;
+use Crypt;
+/**
+ * Class AttachmentController
+ *
+ * @package FireflyIII\Http\Controllers
+ */
+class AttachmentController extends Controller
+{
+
+ /**
+ * @param Attachment $attachment
+ */
+ public function download(Attachment $attachment, AttachmentHelperInterface $helper)
+ {
+
+ $file = $helper->getAttachmentLocation($attachment);
+ if (file_exists($file)) {
+
+ $quoted = sprintf('"%s"', addcslashes(basename($attachment->filename), '"\\'));
+
+ header('Content-Description: File Transfer');
+ header('Content-Type: application/octet-stream');
+ header('Content-Disposition: attachment; filename=' . $quoted);
+ header('Content-Transfer-Encoding: binary');
+ header('Connection: Keep-Alive');
+ header('Expires: 0');
+ header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
+ header('Pragma: public');
+ header('Content-Length: ' . $attachment->size);
+
+ echo Crypt::decrypt(file_get_contents($file));
+
+ } else {
+ abort(404);
+ }
+
+
+ }
+
+}
\ No newline at end of file
diff --git a/app/Http/routes.php b/app/Http/routes.php
index aa6d5d0daa..c638e56850 100644
--- a/app/Http/routes.php
+++ b/app/Http/routes.php
@@ -1,5 +1,6 @@
<?php
use FireflyIII\Models\Account;
+use FireflyIII\Models\Attachment;
use FireflyIII\Models\Bill;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
@@ -42,6 +43,19 @@ Route::bind(
}
);
+Route::bind(
+ 'attachment', function ($value) {
+ if (Auth::check()) {
+ $object = Attachment::where('id', $value)->where('user_id', Auth::user()->id)->first();
+ if ($object) {
+ return $object;
+ }
+ }
+
+ throw new NotFoundHttpException;
+}
+);
+
Route::bind(
'currency', function ($value) {
if (Auth::check()) {
@@ -177,6 +191,12 @@ Route::group(
Route::post('/accounts/update/{account}', ['uses' => 'AccountController@update', 'as' => 'accounts.update']);
Route::post('/accounts/destroy/{account}', ['uses' => 'AccountController@destroy', 'as' => 'accounts.destroy']);
+ /**
+ * Attachment Controller
+ */
+ Route::get('/attachment/{attachment}/download', ['uses' => 'AttachmentController@download', 'as' => 'attachment.download']);
+
+
/**
* Bills Controller
*/
diff --git a/app/Models/Attachment.php b/app/Models/Attachment.php
index 1df6a57025..4384abdc17 100644
--- a/app/Models/Attachment.php
+++ b/app/Models/Attachment.php
@@ -9,6 +9,32 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* Class Attachment
*
* @package FireflyIII\Models
+ * @property integer $id
+ * @property \Carbon\Carbon $created_at
+ * @property \Carbon\Carbon $updated_at
+ * @property string $deleted_at
+ * @property integer $attachable_id
+ * @property string $attachable_type
+ * @property integer $user_id
+ * @property string $md5
+ * @property string $filename
+ * @property string $mime
+ * @property integer $size
+ * @property boolean $uploaded
+ * @property-read \ $attachable
+ * @property-read \FireflyIII\User $user
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereId($value)
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereCreatedAt($value)
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereUpdatedAt($value)
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereDeletedAt($value)
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereAttachableId($value)
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereAttachableType($value)
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereUserId($value)
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereMd5($value)
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereFilename($value)
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereMime($value)
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereSize($value)
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereUploaded($value)
*/
class Attachment extends Model
{
diff --git a/resources/twig/transactions/show.twig b/resources/twig/transactions/show.twig
index cad7844a82..1e9476c747 100644
--- a/resources/twig/transactions/show.twig
+++ b/resources/twig/transactions/show.twig
@@ -89,7 +89,7 @@
</td>
<td>
<i class="fa fa-file-pdf-o"></i>
- <a href="#">{{ att.filename }}</a>
+ <a href="{{ route('attachment.download', att.id) }}">{{ att.filename }}</a>
({{ att.size|filesize }})
</td>
</tr>