Skip to content
This repository was archived by the owner on May 7, 2019. It is now read-only.

Commit 5668c46

Browse files
Fix EXIF not actually working due to saving the resulting file to the wrong directory
1 parent 603b68e commit 5668c46

File tree

4 files changed

+215
-191
lines changed

4 files changed

+215
-191
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Please view this file on the master branch, on stable branches it's out of date.
22

3-
v 1.0.4 (unreleased)
3+
v 1.0.4
44
- Don't call save() after create(), it's redundant.
5+
- Actually strip EXIF info from images and save them to the right file.
56

67
v 1.0.3
78
- Make the example nginx config a little safer.

app/Http/Controllers/Api/UploadController.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,9 @@ public function post(Request $request)
7272
$newName = str_random($randomLen++) . ".$ext";
7373
} while (Upload::whereName($newName)->first() || $newName === 'index.php');
7474

75-
$upload = Upload::create([
75+
$upload = new Upload([
7676
'user_id' => Auth::id(),
77-
'hash' => sha1_file($file),
7877
'name' => $newName,
79-
'size' => $file->getSize(),
8078
'original_name' => $originalName,
8179
'original_hash' => $originalHash
8280
]);
@@ -95,21 +93,33 @@ public function post(Request $request)
9593
return response()->json([trans('messages.unsupported_image_type')], StatusCode::INTERNAL_SERVER_ERROR);
9694
}
9795

98-
$img->backup();
99-
$img->resize(128, 128)->save($upload->getThumbnailPath(true));
100-
$img->reset();
96+
try {
97+
$upload->createDirs();
98+
$img->backup();
99+
$img->resize(128, 128)->save($upload->getThumbnailPath(true));
100+
$img->reset();
101+
} catch (NotWritableException $ex) {
102+
Log::error($ex);
103+
$upload->deleteDirs();
104+
return response()->json([trans('messages.could_not_write_image')], StatusCode::INTERNAL_SERVER_ERROR);
105+
}
101106

102107
if (Helpers::shouldStripExif($file)) {
103108
try {
104-
$img->save($file, 100);
109+
$img->save($upload->getPath(true));
105110
} catch (NotWritableException $ex) {
106111
Log::error($ex);
112+
$upload->deleteDirs();
107113
return response()->json([trans('messages.could_not_write_image')], StatusCode::INTERNAL_SERVER_ERROR);
108114
}
109115
}
110116
$img->destroy();
111117
}
112118

119+
$upload->hash = sha1_file($upload->getPath(true));
120+
$upload->size = filesize($upload->getPath(true));
121+
$upload->save();
122+
113123
$result = [
114124
'url' => route('files.get', $upload)
115125
];

app/Models/Upload.php

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,18 @@ class Upload extends Model
4040
* @var array
4141
*/
4242
protected $fillable = ['name', 'hash', 'size', 'user_id', 'original_name', 'original_hash', 'downloads', 'views'];
43-
4443
/**
4544
* The attributes excluded from the model's JSON form.
4645
*
4746
* @var array
4847
*/
4948
protected $hidden = ['user_id', 'id'];
5049

51-
public function save(array $options = [])
50+
public static function create(array $attributes = [])
5251
{
53-
if (!Storage::exists($this->getDir())) {
54-
Storage::makeDirectory($this->getDir());
55-
}
52+
Helpers::invalidateCache();
5653

57-
if (!Storage::exists($this->getThumbnailDir())) {
58-
Storage::makeDirectory($this->getThumbnailDir());
59-
}
60-
return parent::save($options);
54+
return parent::create($attributes);
6155
}
6256

6357
/**
@@ -68,36 +62,18 @@ public function user()
6862
return $this->belongsTo('App\Models\User');
6963
}
7064

71-
public static function create(array $attributes = [])
65+
public function createDirs()
7266
{
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());
8169
}
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-
}
8870

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());
9273
}
93-
return sprintf('thumbnails/%s/%s/', md5($this->user_id), md5(mb_substr($this->name, 0, 1, 'utf-8')));
9474
}
9575

96-
public function getThumbnailPath($fullPath = false) {
97-
return $this->getThumbnailDir($fullPath) . $this->name;
98-
}
99-
100-
public function forceDelete()
76+
public function deleteDirs()
10177
{
10278
if (Storage::exists($this->getPath())) {
10379
Storage::delete($this->getPath());
@@ -112,17 +88,50 @@ public function forceDelete()
11288
Storage::deleteDir($this->getThumbnailDir());
11389
}
11490
}
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+
}
115107

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();
116114
Helpers::invalidateCache();
117115

118116
return parent::forceDelete();
119117
}
120118

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+
121129
/**
122130
* Migrates files from the old uploads/$filename structure to a more efficient one.
123131
* Should only be called once from the command line.
124132
*/
125-
public function migrate() {
133+
public function migrate()
134+
{
126135
if (php_sapi_name() !== 'cli') {
127136
return;
128137
}
@@ -149,6 +158,7 @@ public function getThumbnail()
149158
if (Storage::exists($this->getThumbnailDir() . $this->name)) {
150159
return route('account.uploads.thumbnail', $this);
151160
}
161+
152162
return elixir('assets/img/thumbnail.png');
153163
}
154164

0 commit comments

Comments
 (0)