|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Epesi\FileStorage\Database\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Facades\Storage; |
|
7
|
|
|
|
|
8
|
|
|
class WriteError extends \Exception {} |
|
9
|
|
|
|
|
10
|
|
|
class FileContent extends Model { |
|
11
|
|
|
|
|
12
|
|
|
const HASH_METHOD = 'sha512'; |
|
13
|
|
|
|
|
14
|
|
|
public $timestamps = false; |
|
15
|
|
|
|
|
16
|
|
|
protected $table = 'filestorage_contents'; |
|
17
|
|
|
protected static $unguarded = true; |
|
18
|
|
|
|
|
19
|
|
|
protected $appends = ['path']; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* One content can have many files associated with |
|
23
|
|
|
* The actual content is stored only once based on the content hash |
|
24
|
|
|
* |
|
25
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
26
|
|
|
*/ |
|
27
|
|
|
public function files() |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->hasMany(File::class, 'content_id'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Accessor method for retrieving of file content path when using the model |
|
34
|
|
|
* Having the $appends property in the File model listing the 'path' makes sure the value is also exported to arrays |
|
35
|
|
|
* |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getPathAttribute() |
|
39
|
|
|
{ |
|
40
|
|
|
return self::storage()->path($this->getStoragePath($this->hash)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Accessor method for retrieving of file contents |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getDataAttribute() |
|
49
|
|
|
{ |
|
50
|
|
|
return self::storage()->get($this->storage_path); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Accessor method for file relative storage path |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getStoragePathAttribute() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->getStoragePath($this->hash); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected static function getStoragePath($hash) |
|
64
|
|
|
{ |
|
65
|
|
|
return implode(DIRECTORY_SEPARATOR, array_merge(str_split(substr($hash, 0, 5)), [substr($hash, 5)])); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns the storage where file contents are saved based on config settings |
|
70
|
|
|
* |
|
71
|
|
|
* @return \Illuminate\Contracts\Filesystem\Filesystem |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function storage() |
|
74
|
|
|
{ |
|
75
|
|
|
return Storage::disk(config('epesi.filestorage', 'local')); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Add file to the filestorage |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $file File path to save |
|
82
|
|
|
* |
|
83
|
|
|
* @return int File id in the database |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function putFromFile($file) |
|
86
|
|
|
{ |
|
87
|
|
|
return self::put(file_get_contents($file)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Add content to the filestorage |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $content Content to save |
|
94
|
|
|
* |
|
95
|
|
|
* @return int File id in the database |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function put($content) |
|
98
|
|
|
{ |
|
99
|
|
|
$hash = self::hash($content); |
|
100
|
|
|
|
|
101
|
|
|
$path = self::getStoragePath($hash); |
|
102
|
|
|
|
|
103
|
|
|
if (! self::storage()->exists($path)) { |
|
104
|
|
|
self::storage()->put($path, $content); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return self::firstOrCreate(compact('hash'), [ |
|
108
|
|
|
'size' => self::storage()->size($path), |
|
109
|
|
|
'type' => self::storage()->mimeType($path) |
|
110
|
|
|
])->id; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get the hash of the content using hash method defined as constant to the class |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $content |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function hash($content) |
|
120
|
|
|
{ |
|
121
|
|
|
return hash(self::HASH_METHOD, $content); |
|
122
|
|
|
} |
|
123
|
|
|
} |