Laravel Spark上传头像到外部驱动
Laravel Spark upload profile picture to external driver
我想覆盖 Laravel Spark 保存用户个人资料图片的方式,以使用外部驱动程序(例如 S3)。我已经有了要使用的存储桶的 S3 配置。最好的方法是什么?我应该使用完全不同的路由并使用自定义端点,还是我可以更改某处的配置以便 Spark 使用不同的驱动程序?
所以最后做了这个
我在更新配置文件中添加了这些方法-photo.js
methods: {
updateProfilePhoto() {
axios.post('/settings/profile/details/profile-picture', this.gatherFormData())
.then(
() => {
console.log('Profile picture updated');
Bus.$emit('updateUser');
self.form.finishProcessing();
},
(error) => {
self.form.setErrors(error.response.data.errors);
}
);
},
gatherFormData() {
const data = new FormData();
data.append('photo', this.$refs.photo.files[0]);
return data;
}
}
我的控制器看起来像这样
public function updateProfilePicture(Request $request)
{
$this->validate($request, [
'photo' => 'required',
]);
// Storing the photo
//get filename with extension
$filenamewithextension = $request->file('photo')->getClientOriginalName();
//get filename without extension
$filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
//get file extension
$extension = $request->file('photo')->getClientOriginalExtension();
//filename to store
$filenametostore = $filename.'_'.time().'.'.$extension;
Storage::disk('s3_users')->put($filenametostore, fopen($request->file('photo'), 'r+'), 'public');
$url = $filenametostore;
$request->user()->forceFill([
'image_url' => $url
])->save();
return response()->json(
array(
"message" => "Profile picture was updated!",
)
);
}
我想覆盖 Laravel Spark 保存用户个人资料图片的方式,以使用外部驱动程序(例如 S3)。我已经有了要使用的存储桶的 S3 配置。最好的方法是什么?我应该使用完全不同的路由并使用自定义端点,还是我可以更改某处的配置以便 Spark 使用不同的驱动程序?
所以最后做了这个
我在更新配置文件中添加了这些方法-photo.js
methods: {
updateProfilePhoto() {
axios.post('/settings/profile/details/profile-picture', this.gatherFormData())
.then(
() => {
console.log('Profile picture updated');
Bus.$emit('updateUser');
self.form.finishProcessing();
},
(error) => {
self.form.setErrors(error.response.data.errors);
}
);
},
gatherFormData() {
const data = new FormData();
data.append('photo', this.$refs.photo.files[0]);
return data;
}
}
我的控制器看起来像这样
public function updateProfilePicture(Request $request)
{
$this->validate($request, [
'photo' => 'required',
]);
// Storing the photo
//get filename with extension
$filenamewithextension = $request->file('photo')->getClientOriginalName();
//get filename without extension
$filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
//get file extension
$extension = $request->file('photo')->getClientOriginalExtension();
//filename to store
$filenametostore = $filename.'_'.time().'.'.$extension;
Storage::disk('s3_users')->put($filenametostore, fopen($request->file('photo'), 'r+'), 'public');
$url = $filenametostore;
$request->user()->forceFill([
'image_url' => $url
])->save();
return response()->json(
array(
"message" => "Profile picture was updated!",
)
);
}