Skip to content

Commit 9d6bb3c

Browse files
committed
re
1 parent 1635182 commit 9d6bb3c

File tree

5 files changed

+89
-186
lines changed

5 files changed

+89
-186
lines changed

config/attachmentable.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
22

3+
use Illuminate\Support\Facades\Storage;
4+
35
// config file for laravelir/attachmentable
46
return [
57

68
/**
79
* use your favorite filesystem.php disk
810
*/
9-
'disk' => env('ATTACHMENTABLE_DISK', 'public'),
11+
'disk' => env('ATTACHMENTABLE_DISK', Storage::getDefaultDriver()),
1012

1113

1214
// All Functionality in this Directory
@@ -26,6 +28,12 @@
2628

2729
'uploads' => [
2830
'default_directory' =>'uploads',
31+
32+
/**
33+
* url : url($path)
34+
*
35+
*/
36+
'uploaded_file_path' => '',
2937
'image' => [
3038
'thumbnail' => [
3139
'width' => '120',

notes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ add attach from url, stream ,
55
add image optimizer
66

77
add jobs, events for upload, image process
8+
9+
add file to attachmentable by specific user logged in or ...

src/Services/AttachmentService.php

Lines changed: 7 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,22 @@ public function attach($file, $attachmentable)
2828
return false;
2929
}
3030

31+
// $this->disk()->put();
32+
3133
$attachmentable->attachments()->create([
3234
'attachmentorable_id' => auth()->user()->id,
3335
'attachmentorable_type' => get_class(auth()->user()),
3436
]);
3537

36-
37-
}
3838

39-
public function name($name)
40-
{
41-
$this->filename = $name;
42-
return $this;
4339
}
4440

45-
46-
4741
public function detach($file)
4842
{
49-
//
43+
$this->disk()->delete($file);
5044
}
5145

52-
public function uploadOneFile(UploadedFile $uploadedFile, $path = null)
46+
public function uploadFile(UploadedFile $uploadedFile, $path = null)
5347
{
5448

5549
$path = $this->path($path);
@@ -89,7 +83,7 @@ public function uploadOneFile(UploadedFile $uploadedFile, $path = null)
8983
return false;
9084
}
9185

92-
public function uploadOneImage(UploadedFile $uploadedFile, $path = null)
86+
public function uploadImage(UploadedFile $uploadedFile, $path = null)
9387
{
9488

9589
$path = $this->path($path);
@@ -130,22 +124,13 @@ public function uploadOneImage(UploadedFile $uploadedFile, $path = null)
130124
return false;
131125
}
132126

133-
public function path($path)
134-
{
135-
return $this->defaultUploadFolderName . $this->ds . $path;
136-
}
137-
138127
function mkdir_if_not_exists($dirPath)
139128
{
140129
if (!file_exists($dirPath)) {
141130
mkdir($dirPath, 0777, true);
142131
}
143132
}
144133

145-
public function deleteOne($folder = null, $filename = null, $disk = null)
146-
{
147-
$this->disk()->delete($folder . $filename);
148-
}
149134

150135

151136
/**
@@ -162,73 +147,22 @@ public function fromPost($uploadedFile, $disk = null)
162147
return null;
163148
}
164149

165-
$this->disk = $this->disk ?: ($disk ?: Storage::getDefaultDriver());
166150
$this->filename = $uploadedFile->getClientOriginalName();
167151
$this->filesize = method_exists($uploadedFile, 'getSize') ? $uploadedFile->getSize() : $uploadedFile->getClientSize();
168152
$this->filetype = $uploadedFile->getMimeType();
169153
$this->filepath = $this->filepath ?: ($this->getStorageDirectory() . $this->getPartitionDirectory() . $this->getDiskName());
170-
$this->putFile($uploadedFile->getRealPath(), $this->filepath);
171-
172-
return $this;
173-
}
174-
175-
176-
/**
177-
* Creates a file object from a file on the disk.
178-
*
179-
* @param string $filePath source file
180-
* @param string $disk target storage disk
181-
*
182-
* @return $this|null
183-
*/
184-
public function fromFile($filePath, $disk = null)
185-
{
186-
if ($filePath === null) {
187-
return null;
188-
}
189-
190-
$file = new FileObj($filePath);
191-
192-
$this->disk = $this->disk ?: ($disk ?: Storage::getDefaultDriver());
193154
$this->filename = $file->getFilename();
194155
$this->filesize = $file->getSize();
195156
$this->filetype = $file->getMimeType();
196157
$this->filepath = $this->filepath ?: ($this->getStorageDirectory() . $this->getPartitionDirectory() . $this->getDiskName());
197158
$this->putFile($file->getRealPath(), $this->filepath);
198159

160+
$this->putFile($uploadedFile->getRealPath(), $this->filepath);
161+
199162
return $this;
200163
}
201164

202165

203-
/**
204-
* Creates a file object from a stream
205-
*
206-
* @param resource $stream source stream
207-
* @param string $filename the resource filename
208-
* @param string $disk target storage disk
209-
*
210-
* @return $this|null
211-
*/
212-
public function fromStream($stream, $filename, $disk = null)
213-
{
214-
if ($stream === null) {
215-
return null;
216-
}
217-
218-
$this->disk = $this->disk ?: ($disk ?: Storage::getDefaultDriver());
219-
220-
$driver = Storage::disk($this->disk);
221-
222-
$this->filename = $filename;
223-
$this->filepath = $this->filepath ?: ($this->getStorageDirectory() . $this->getPartitionDirectory() . $this->getDiskName());
224-
225-
$driver->putStream($this->filepath, $stream);
226-
227-
$this->filesize = $driver->size($this->filepath);
228-
$this->filetype = $driver->mimeType($this->filepath);
229-
230-
return $this;
231-
}
232166

233167
/**
234168
* Register an outputting model event with the dispatcher.
@@ -242,100 +176,6 @@ public static function outputting($callback)
242176
static::registerModelEvent('outputting', $callback);
243177
}
244178

245-
public function getUuidAttribute()
246-
{
247-
if ( ! empty($this->attributes['uuid'])) {
248-
return $this->attributes['uuid'];
249-
}
250-
251-
$generator = config('attachments.uuid_provider');
252-
253-
if (strpos($generator, '@') !== false) {
254-
$generator = explode('@', $generator, 2);
255-
}
256-
257-
if ( ! is_array($generator) && function_exists($generator)) {
258-
return $this->uuid = call_user_func($generator);
259-
}
260-
261-
if (is_callable($generator)) {
262-
return $this->uuid = forward_static_call($generator);
263-
}
264-
265-
throw new \Exception('Missing UUID provider configuration for attachments');
266-
}
267-
268-
269-
public function getExtensionAttribute()
270-
{
271-
return pathinfo($this->original_name, PATHINFO_EXTENSION);
272-
}
273-
274-
275-
public function getPathAttribute()
276-
{
277-
return pathinfo($this->filepath, PATHINFO_DIRNAME);
278-
}
279-
280-
281-
public function getUrlAttribute()
282-
{
283-
if ($this->isLocalStorage()) {
284-
return $this->proxy_url;
285-
} else {
286-
return Storage::disk($this->disk)->url($this->filepath);
287-
}
288-
}
289-
290-
291-
public function getUrlInlineAttribute()
292-
{
293-
if ($this->isLocalStorage()) {
294-
return $this->proxy_url_inline;
295-
} else {
296-
return Storage::disk($this->disk)->url($this->filepath);
297-
}
298-
}
299-
300-
301-
public function getProxyUrlAttribute()
302-
{
303-
return route('attachments.download', [
304-
'id' => $this->uuid,
305-
'name' => $this->extension ?
306-
Str::slug(substr($this->filename, 0, -1 * strlen($this->extension) - 1)) . '.' . $this->extension :
307-
Str::slug($this->filename)
308-
]);
309-
}
310-
311-
312-
public function getProxyUrlInlineAttribute()
313-
{
314-
return route('attachments.download', [
315-
'id' => $this->uuid,
316-
'name' => $this->extension ?
317-
Str::slug(substr($this->filename, 0, -1 * strlen($this->extension) - 1)) . '.' . $this->extension :
318-
Str::slug($this->filename),
319-
'disposition' => 'inline',
320-
]);
321-
}
322-
323-
324-
public function toArray()
325-
{
326-
$attributes = parent::toArray();
327-
328-
return array_merge($attributes, [
329-
'url' => $this->url,
330-
'url_inline' => $this->url_inline,
331-
]);
332-
}
333-
334-
335-
/*
336-
* File handling
337-
*/
338-
339179
public function output($disposition = 'inline')
340180
{
341181
if ($this->fireModelEvent('outputting') === false) {
@@ -479,19 +319,6 @@ protected function getStorageDirectory()
479319
}
480320

481321

482-
/**
483-
* If working with local storage, determine the absolute local path.
484-
*
485-
* @return string
486-
*/
487-
protected function getLocalRootPath()
488-
{
489-
return storage_path() . '/app';
490-
}
491-
492-
493-
494-
495322
/**
496323
* Returns true if a directory contains no files.
497324
*
@@ -590,9 +417,4 @@ protected function storageCommand($string, $filepath)
590417
return forward_static_call_array([$interface, $command], $args);
591418
}
592419

593-
594-
public function getConnectionName()
595-
{
596-
return config('attachments.database.connection') ?? $this->connection;
597-
}
598420
}

src/Services/Service.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ public function disk()
3939
return Storage::disk($this->disk);
4040
}
4141

42+
/**
43+
* return upload path.
44+
*
45+
* @return string
46+
*/
47+
protected function path()
48+
{
49+
return storage_path() . '/app';
50+
}
51+
52+
public function path2($path)
53+
{
54+
return $this->defaultUploadFolderName . $this->ds . $path;
55+
}
56+
4257
protected function isLocalStorage(): bool
4358
{
4459
return $this->disk == 'local';
@@ -48,4 +63,26 @@ protected function isPublicStorage(): bool
4863
{
4964
return $this->disk == 'public';
5065
}
66+
67+
public function getFileExtension($file)
68+
{
69+
return pathinfo($file->original_name, PATHINFO_EXTENSION);
70+
}
71+
72+
public function getFilePath($file)
73+
{
74+
return pathinfo($file->filepath, PATHINFO_DIRNAME);
75+
}
76+
77+
public function getFileUrl($file)
78+
{
79+
if ($this->isLocalStorage()) {
80+
return $this->proxy_url;
81+
} else {
82+
return $this->disk()->url($file->filepath);
83+
}
84+
}
85+
86+
87+
5188
}

src/Services/mt.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
4+
5+
6+
/**
7+
* Creates a file object from a stream
8+
*
9+
* @param resource $stream source stream
10+
* @param string $filename the resource filename
11+
* @param string $disk target storage disk
12+
*
13+
* @return $this|null
14+
*/
15+
public function fromStream($stream, $filename, $disk = null)
16+
{
17+
if ($stream === null) {
18+
return null;
19+
}
20+
21+
$this->disk = $this->disk ?: ($disk ?: Storage::getDefaultDriver());
22+
23+
$driver = Storage::disk($this->disk);
24+
25+
$this->filename = $filename;
26+
$this->filepath = $this->filepath ?: ($this->getStorageDirectory() . $this->getPartitionDirectory() . $this->getDiskName());
27+
28+
$driver->putStream($this->filepath, $stream);
29+
30+
$this->filesize = $driver->size($this->filepath);
31+
$this->filetype = $driver->mimeType($this->filepath);
32+
33+
return $this;
34+
}

0 commit comments

Comments
 (0)