mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Basic attachment download function.
This commit is contained in:
parent
359fab315f
commit
cc1af60cb4
@ -20,7 +20,7 @@ class AttachmentHelper implements AttachmentHelperInterface
|
|||||||
|
|
||||||
// move to config:
|
// move to config:
|
||||||
protected $maxUploadSize = 1048576; // 1MB per file
|
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 $errors;
|
||||||
public $messages;
|
public $messages;
|
||||||
@ -34,6 +34,18 @@ class AttachmentHelper implements AttachmentHelperInterface
|
|||||||
$this->messages = new MessageBag;
|
$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
|
* @param Model $model
|
||||||
*
|
*
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace FireflyIII\Helpers\Attachments;
|
namespace FireflyIII\Helpers\Attachments;
|
||||||
|
|
||||||
|
use FireflyIII\Models\Attachment;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\MessageBag;
|
use Illuminate\Support\MessageBag;
|
||||||
|
|
||||||
@ -30,4 +31,11 @@ interface AttachmentHelperInterface
|
|||||||
*/
|
*/
|
||||||
public function getMessages();
|
public function getMessages();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Attachment $attachment
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getAttachmentLocation(Attachment $attachment);
|
||||||
|
|
||||||
}
|
}
|
47
app/Http/Controllers/AttachmentController.php
Normal file
47
app/Http/Controllers/AttachmentController.php
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
|
use FireflyIII\Models\Attachment;
|
||||||
use FireflyIII\Models\Bill;
|
use FireflyIII\Models\Bill;
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
use FireflyIII\Models\Category;
|
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(
|
Route::bind(
|
||||||
'currency', function ($value) {
|
'currency', function ($value) {
|
||||||
if (Auth::check()) {
|
if (Auth::check()) {
|
||||||
@ -177,6 +191,12 @@ Route::group(
|
|||||||
Route::post('/accounts/update/{account}', ['uses' => 'AccountController@update', 'as' => 'accounts.update']);
|
Route::post('/accounts/update/{account}', ['uses' => 'AccountController@update', 'as' => 'accounts.update']);
|
||||||
Route::post('/accounts/destroy/{account}', ['uses' => 'AccountController@destroy', 'as' => 'accounts.destroy']);
|
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
|
* Bills Controller
|
||||||
*/
|
*/
|
||||||
|
@ -9,6 +9,32 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
|||||||
* Class Attachment
|
* Class Attachment
|
||||||
*
|
*
|
||||||
* @package FireflyIII\Models
|
* @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
|
class Attachment extends Model
|
||||||
{
|
{
|
||||||
|
@ -89,7 +89,7 @@
|
|||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<i class="fa fa-file-pdf-o"></i>
|
<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 }})
|
({{ att.size|filesize }})
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
Loading…
Reference in New Issue
Block a user