@@ -40,24 +40,18 @@ class Upload extends Model
40
40
* @var array
41
41
*/
42
42
protected $ fillable = ['name ' , 'hash ' , 'size ' , 'user_id ' , 'original_name ' , 'original_hash ' , 'downloads ' , 'views ' ];
43
-
44
43
/**
45
44
* The attributes excluded from the model's JSON form.
46
45
*
47
46
* @var array
48
47
*/
49
48
protected $ hidden = ['user_id ' , 'id ' ];
50
49
51
- public function save (array $ options = [])
50
+ public static function create (array $ attributes = [])
52
51
{
53
- if (!Storage::exists ($ this ->getDir ())) {
54
- Storage::makeDirectory ($ this ->getDir ());
55
- }
52
+ Helpers::invalidateCache ();
56
53
57
- if (!Storage::exists ($ this ->getThumbnailDir ())) {
58
- Storage::makeDirectory ($ this ->getThumbnailDir ());
59
- }
60
- return parent ::save ($ options );
54
+ return parent ::create ($ attributes );
61
55
}
62
56
63
57
/**
@@ -68,36 +62,18 @@ public function user()
68
62
return $ this ->belongsTo ('App\Models\User ' );
69
63
}
70
64
71
- public static function create ( array $ attributes = [] )
65
+ public function createDirs ( )
72
66
{
73
- Helpers::invalidateCache ();
74
-
75
- return parent ::create ($ attributes );
76
- }
77
-
78
- public function getDir ($ fullDir = false ) {
79
- if ($ fullDir ) {
80
- return storage_path (sprintf ('app/uploads/%s/%s/ ' , md5 ($ this ->user_id ), md5 (mb_substr ($ this ->name , 0 , 1 , 'utf-8 ' ))));
67
+ if (!Storage::exists ($ this ->getDir ())) {
68
+ Storage::makeDirectory ($ this ->getDir ());
81
69
}
82
- return sprintf ('uploads/%s/%s/ ' , md5 ($ this ->user_id ), md5 (mb_substr ($ this ->name , 0 , 1 , 'utf-8 ' )));
83
- }
84
-
85
- public function getPath ($ fullPath = false ) {
86
- return $ this ->getDir ($ fullPath ) . $ this ->name ;
87
- }
88
70
89
- public function getThumbnailDir ($ fullDir = false ) {
90
- if ($ fullDir ) {
91
- return storage_path (sprintf ('app/thumbnails/%s/%s/ ' , md5 ($ this ->user_id ), md5 (mb_substr ($ this ->name , 0 , 1 , 'utf-8 ' ))));
71
+ if (!Storage::exists ($ this ->getThumbnailDir ())) {
72
+ Storage::makeDirectory ($ this ->getThumbnailDir ());
92
73
}
93
- return sprintf ('thumbnails/%s/%s/ ' , md5 ($ this ->user_id ), md5 (mb_substr ($ this ->name , 0 , 1 , 'utf-8 ' )));
94
74
}
95
75
96
- public function getThumbnailPath ($ fullPath = false ) {
97
- return $ this ->getThumbnailDir ($ fullPath ) . $ this ->name ;
98
- }
99
-
100
- public function forceDelete ()
76
+ public function deleteDirs ()
101
77
{
102
78
if (Storage::exists ($ this ->getPath ())) {
103
79
Storage::delete ($ this ->getPath ());
@@ -112,17 +88,50 @@ public function forceDelete()
112
88
Storage::deleteDir ($ this ->getThumbnailDir ());
113
89
}
114
90
}
91
+ }
92
+
93
+ public function getDir ($ fullDir = false )
94
+ {
95
+ if ($ fullDir ) {
96
+ return storage_path (sprintf ('app/uploads/%s/%s/ ' , md5 ($ this ->user_id ), md5 (mb_substr ($ this ->name , 0 , 1 , 'utf-8 ' ))));
97
+ }
98
+
99
+ return sprintf ('uploads/%s/%s/ ' , md5 ($ this ->user_id ), md5 (mb_substr ($ this ->name , 0 , 1 , 'utf-8 ' )));
100
+ }
101
+
102
+ public function getThumbnailDir ($ fullDir = false )
103
+ {
104
+ if ($ fullDir ) {
105
+ return storage_path (sprintf ('app/thumbnails/%s/%s/ ' , md5 ($ this ->user_id ), md5 (mb_substr ($ this ->name , 0 , 1 , 'utf-8 ' ))));
106
+ }
115
107
108
+ return sprintf ('thumbnails/%s/%s/ ' , md5 ($ this ->user_id ), md5 (mb_substr ($ this ->name , 0 , 1 , 'utf-8 ' )));
109
+ }
110
+
111
+ public function forceDelete ()
112
+ {
113
+ $ this ->deleteDirs ();
116
114
Helpers::invalidateCache ();
117
115
118
116
return parent ::forceDelete ();
119
117
}
120
118
119
+ public function getPath ($ fullPath = false )
120
+ {
121
+ return $ this ->getDir ($ fullPath ) . $ this ->name ;
122
+ }
123
+
124
+ public function getThumbnailPath ($ fullPath = false )
125
+ {
126
+ return $ this ->getThumbnailDir ($ fullPath ) . $ this ->name ;
127
+ }
128
+
121
129
/**
122
130
* Migrates files from the old uploads/$filename structure to a more efficient one.
123
131
* Should only be called once from the command line.
124
132
*/
125
- public function migrate () {
133
+ public function migrate ()
134
+ {
126
135
if (php_sapi_name () !== 'cli ' ) {
127
136
return ;
128
137
}
@@ -149,6 +158,7 @@ public function getThumbnail()
149
158
if (Storage::exists ($ this ->getThumbnailDir () . $ this ->name )) {
150
159
return route ('account.uploads.thumbnail ' , $ this );
151
160
}
161
+
152
162
return elixir ('assets/img/thumbnail.png ' );
153
163
}
154
164
0 commit comments