Skip to content

Commit c1c4696

Browse files
committed
refactoring
1 parent 3e3b5ee commit c1c4696

File tree

6 files changed

+26
-91
lines changed

6 files changed

+26
-91
lines changed

src/Services/AttachmentService.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Http\UploadedFile;
88
use Intervention\Image\Facades\Image;
9+
use Laravelir\Attachmentable\Models\Attachment;
910

1011

1112
final class AttachmentService extends Service
@@ -46,7 +47,6 @@ public function attach($file, $attachmentable)
4647
'disk' => $this->disk,
4748
]);
4849

49-
5050
}
5151

5252
public function detach($file)
@@ -138,20 +138,6 @@ function mkdir_if_not_exists($dirPath)
138138
}
139139
}
140140

141-
public function output($disposition = 'inline')
142-
{
143-
144-
header("Content-type: " . $this->filetype);
145-
header('Content-Disposition: ' . $disposition . '; filename="' . $this->filename . '"');
146-
header('Cache-Control: private');
147-
header('Cache-Control: no-store, no-cache, must-revalidate');
148-
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
149-
header('Accept-Ranges: bytes');
150-
header('Content-Length: ' . $this->filesize);
151-
152-
exit($this->getContents());
153-
}
154-
155141
public function isFile($file): bool
156142
{
157143
return true;
@@ -162,5 +148,4 @@ public function isImage($file): bool
162148
return true;
163149
}
164150

165-
166151
}

src/Services/DownloadService.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@ public function __construct()
1313

1414
public function download($id, Request $request)
1515
{
16-
$disposition = ($disposition = $request->input('disposition')) === 'inline' ? $disposition : 'attachment';
17-
18-
if ($file = $this->model->where('uuid', $id)->first()) {
19-
try {
20-
if (!$file->output($disposition)) {
21-
abort(403, Lang::get('attachments::messages.errors.access_denied'));
22-
}
23-
} catch (FileNotFoundException $e) {
24-
abort(404, Lang::get('attachments::messages.errors.file_not_found'));
25-
}
26-
}
27-
28-
abort(404, Lang::get('attachments::messages.errors.file_not_found'));
16+
if ($file = $this->model->where('uuid', $id)->first()) {}
2917
}
3018

19+
public function output($disposition = 'inline')
20+
{
21+
22+
header("Content-type: " . $this->filetype);
23+
header('Content-Disposition: ' . $disposition . '; filename="' . $this->filename . '"');
24+
header('Cache-Control: private');
25+
header('Cache-Control: no-store, no-cache, must-revalidate');
26+
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
27+
header('Accept-Ranges: bytes');
28+
header('Content-Length: ' . $this->filesize);
29+
30+
exit($this->getContents());
31+
}
3132

3233
}

src/Services/Service.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public function getFileUrl($file)
8282
}
8383
}
8484

85-
8685
public function setFileMetadata($file)
8786
{
8887
$data = [

src/Services/mt.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,8 @@
3333
// return $this;
3434
//}
3535

36-
36+
//
37+
//public function attachFromUrl($url, array $options = null)
38+
//{
39+
// # code...
40+
//}

src/Traits/Attachmentable.php

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
namespace Laravelir\Attachmentable\Traits;
44

55
use Illuminate\Database\Eloquent\Relations\MorphMany;
6-
use Illuminate\Support\Arr;
7-
use Illuminate\Http\Request;
86
use Illuminate\Http\UploadedFile;
9-
use Illuminate\Support\Facades\Lang;
107
use Laravelir\Attachmentable\Models\Attachment;
11-
use Illuminate\Contracts\Filesystem\FileNotFoundException;
128
use Laravelir\Attachmentable\Services\AttachmentService;
139

1410
trait Attachmentable
@@ -40,59 +36,18 @@ public function attach($file, $path = null): bool
4036
return false;
4137
}
4238

43-
public function hasAttachment($key)
39+
public function detach(): bool
4440
{
45-
# code...
41+
return true;
4642
}
4743

48-
public function clearAttachments($group)
44+
public function hasAttachment($key): bool
4945
{
50-
# code...
46+
return true;
5147
}
5248

53-
public function attachFromUrl($url, array $options = null)
49+
public function clearAttachments(): bool
5450
{
55-
# code...
51+
return true;
5652
}
57-
58-
public static function attach3($uuid, $model, $options = [])
59-
{
60-
/** @var Attachment $attachment */
61-
$attachment = self::where('uuid', $uuid)->first();
62-
63-
if (!$attachment) {
64-
return null;
65-
}
66-
67-
// The dz_session_key is set by the build-in DropzoneController for security check
68-
if ($attachment->metadata('dz_session_key')) {
69-
$meta = $attachment->metadata;
70-
71-
unset($meta['dz_session_key']);
72-
73-
$attachment->metadata = $meta;
74-
}
75-
76-
$options = Arr::only($options, config('attachments.attributes'));
77-
78-
$attachment->fill($options);
79-
80-
if ($found = $model->attachments()->where('key', '=', $attachment->key)->first()) {
81-
$found->delete();
82-
}
83-
84-
return $attachment->model()->associate($model)->save() ? $attachment : null;
85-
}
86-
87-
public function deleteOne()
88-
{
89-
# code
90-
}
91-
92-
public function delete()
93-
{
94-
# code
95-
}
96-
97-
9853
}

src/Traits/Attachmentorable.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ public function attachments(): MorphMany
1616

1717
public function attach(Model $model, UploadedFile $file, array $option = null): bool
1818
{
19-
if (!$model instanceof Model) {
20-
return false;
21-
}
22-
23-
$model->attachments()->create([
24-
'attachmentorable_id' => $this->id,
25-
'attachmentorable_type' => get_class($this),
26-
]);
27-
2819
return true;
2920
}
3021

0 commit comments

Comments
 (0)