Skip to content

Commit 05d5f80

Browse files
committed
Allow upload files to AWS s3
1 parent 7f1d40e commit 05d5f80

File tree

1 file changed

+52
-20
lines changed

1 file changed

+52
-20
lines changed

src/Helpers/LaravelCmsHelper.php

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ public function imageUrl($img_obj, $width = null, $height = null, $resize_type =
121121

122122
if ('svg' == $img_obj->suffix || (null == $width && null == $height) || ! isset($img_obj->is_image) || false == $img_obj->is_image) {
123123
// return original img url
124+
if ('s3' == config('filesystems.default')) {
125+
return \Storage::url($this->s('file.upload_dir').'/'.$img_obj->path);
126+
}
127+
124128
return '/'.$this->s('file.upload_dir').'/'.$img_obj->path;
125129
}
126130

@@ -137,25 +141,44 @@ public function imageUrl($img_obj, $width = null, $height = null, $resize_type =
137141
$abs_real_dir = public_path($related_dir);
138142
$abs_real_path = $abs_real_dir.'/'.$filename;
139143
$web_url = '/'.$related_dir.'/'.$filename;
144+
if ('s3' == config('filesystems.default')) {
145+
$currentTime = \Carbon\Carbon::now();
146+
$diffInSec = $currentTime->diffInSeconds($img_obj->updated_at);
147+
// add $diffInSec to speed up
148+
if ($diffInSec > 90 || \Storage::exists($related_dir.'/'.$filename)) {
149+
return \Storage::url($related_dir.'/'.$filename);
150+
}
140151

141-
if (file_exists($abs_real_path)) {
142-
$image_reoptimize_time = $this->s('image_reoptimize_time');
143-
if (0 == $image_reoptimize_time) {
144-
return $web_url;
145-
} elseif (filemtime($abs_real_path) > time() - $this->s('image_reoptimize_time')) {
152+
try {
153+
$original_img = \Storage::get(''.$this->s('file.upload_dir').'/'.$img_obj->path);
154+
} catch (\Exception $e) {
155+
$original_img = null;
156+
$web_url .= '?s3error='.$e->getMessage();
157+
}
158+
159+
if (! $original_img) {
146160
return $web_url;
147161
}
148-
//return $abs_real_path . ' - already exists - ' . $web_url;
149-
}
162+
} else {
163+
if (file_exists($abs_real_path)) {
164+
$image_reoptimize_time = $this->s('image_reoptimize_time');
165+
if (0 == $image_reoptimize_time) {
166+
return $web_url;
167+
} elseif (filemtime($abs_real_path) > time() - $this->s('image_reoptimize_time')) {
168+
return $web_url;
169+
}
170+
//return $abs_real_path . ' - already exists - ' . $web_url;
171+
}
150172

151-
if (! file_exists($abs_real_dir)) {
152-
mkdir($abs_real_dir, 0755, true);
153-
}
173+
if (! file_exists($abs_real_dir)) {
174+
mkdir($abs_real_dir, 0755, true);
175+
}
154176

155-
$original_img = public_path(''.$this->s('file.upload_dir').'/'.$img_obj->path);
177+
$original_img = public_path(''.$this->s('file.upload_dir').'/'.$img_obj->path);
156178

157-
if (! file_exists($original_img)) {
158-
return $web_url;
179+
if (! file_exists($original_img)) {
180+
return $web_url.'?original_img_not_exists';
181+
}
159182
}
160183

161184
try {
@@ -167,7 +190,13 @@ public function imageUrl($img_obj, $width = null, $height = null, $resize_type =
167190
if ('jpg' == $suffix || 'jpeg' == $suffix) {
168191
$new_img->encode('jpg');
169192
}
170-
$new_img->save($abs_real_path, 75);
193+
194+
if ('s3' == config('filesystems.default')) {
195+
\Storage::disk('s3')->put($related_dir.'/'.$filename, $new_img);
196+
$web_url = \Storage::url($related_dir.'/'.$filename);
197+
} else {
198+
$new_img->save($abs_real_path, 75);
199+
}
171200
} catch (\Exception $e) {
172201
$web_url .= '?error='.$e->getMessage();
173202
}
@@ -476,15 +505,18 @@ public function uploadFile($f, $options = null)
476505

477506
$file_store_dir = public_path(dirname($this->s('file.upload_dir').'/'.$file_data['path']));
478507

479-
// $this->debug($file_store_dir . '/'. basename($file_data['path']));
508+
if ('s3' == config('filesystems.default')) {
509+
$s3file = $this->s('file.upload_dir').'/'.$file_data['path'];
510+
// exit($s3file);
511+
$f->storeAs(dirname($s3file), basename($s3file));
512+
} else {
513+
if (! file_exists($file_store_dir)) {
514+
mkdir($file_store_dir, 0755, true);
515+
}
480516

481-
// $f->move($file_store_dir, basename($file_data['path']));
482-
if (! file_exists($file_store_dir)) {
483-
mkdir($file_store_dir, 0755, true);
517+
rename($file_data['temp_path'], $file_store_dir.'/'.basename($file_data['path']));
484518
}
485519

486-
rename($file_data['temp_path'], $file_store_dir.'/'.basename($file_data['path']));
487-
488520
return $new_file;
489521

490522
// echo '<pre>111:' . var_export($new_file, true) . '</pre>';

0 commit comments

Comments
 (0)