File tree Expand file tree Collapse file tree 3 files changed +33
-5
lines changed Expand file tree Collapse file tree 3 files changed +33
-5
lines changed Original file line number Diff line number Diff line change @@ -128,10 +128,10 @@ public function fromPost($uploadedFile)
128
128
* @param string $filePath The path to the file.
129
129
* @return static
130
130
*/
131
- public function fromFile ($ filePath )
131
+ public function fromFile ($ filePath, $ filename = null )
132
132
{
133
133
$ file = new FileObj ($ filePath );
134
- $ this ->file_name = $ file ->getFilename ();
134
+ $ this ->file_name = empty ( $ filename ) ? $ file ->getFilename () : $ filename ;
135
135
$ this ->file_size = $ file ->getSize ();
136
136
$ this ->content_type = $ file ->getMimeType ();
137
137
$ this ->disk_name = $ this ->getDiskName ();
@@ -150,10 +150,11 @@ public function fromFile($filePath)
150
150
*/
151
151
public function fromData ($ data , $ filename )
152
152
{
153
- $ tempPath = temp_path ($ filename );
153
+ $ tempName = str_replace ('. ' , '' , uniqid ('' , true )) . '.tmp ' ;
154
+ $ tempPath = temp_path ($ tempName );
154
155
FileHelper::put ($ tempPath , $ data );
155
156
156
- $ file = $ this ->fromFile ($ tempPath );
157
+ $ file = $ this ->fromFile ($ tempPath, basename ( $ filename ) );
157
158
FileHelper::delete ($ tempPath );
158
159
159
160
return $ file ;
Original file line number Diff line number Diff line change @@ -52,6 +52,11 @@ class Model extends EloquentModel implements ModelInterface
52
52
*/
53
53
public $ duplicateCache = true ;
54
54
55
+ /**
56
+ * @var bool Indicates if all string model attributes will be trimmed prior to saving.
57
+ */
58
+ public $ trimStringAttributes = true ;
59
+
55
60
/**
56
61
* @var array The array of models booted events.
57
62
*/
@@ -1243,7 +1248,7 @@ public function setAttribute($key, $value)
1243
1248
/*
1244
1249
* Trim strings
1245
1250
*/
1246
- if (is_string ($ value )) {
1251
+ if ($ this -> trimStringAttributes && is_string ($ value )) {
1247
1252
$ value = trim ($ value );
1248
1253
}
1249
1254
Original file line number Diff line number Diff line change @@ -22,6 +22,28 @@ public function testAddCasts()
22
22
$ this ->assertEquals (['id ' => 'int ' , 'foo ' => 'int ' ], $ model ->getCasts ());
23
23
}
24
24
25
+ public function testStringIsTrimmed ()
26
+ {
27
+ $ name = "Name " ;
28
+ $ nameWithSpace = " $ {name} " ;
29
+ $ model = new TestModelGuarded ();
30
+
31
+ $ model ->name = $ nameWithSpace ;
32
+ $ model ->save ();
33
+
34
+ // Make sure we load the database saved model
35
+ $ model ->refresh ();
36
+ $ this ->assertEquals ($ name , $ model ->name );
37
+
38
+ $ model ->trimStringAttributes = false ;
39
+ $ model ->name = $ nameWithSpace ;
40
+ $ model ->save ();
41
+
42
+ // Refresh the model from the database
43
+ $ model ->refresh ();
44
+ $ this ->assertEquals ($ nameWithSpace , $ model ->name );
45
+ }
46
+
25
47
public function testIsGuarded ()
26
48
{
27
49
$ model = new TestModelGuarded ();
You can’t perform that action at this time.
0 commit comments