i am using ckeditor5 vue version for my article form from this package and it also come with example handling image upload by adding this code
UploadAdapter: function (loader) {
this.loader = loader
this.upload = () => {
const body = new FormData();
body.append('gambar', this.loader.file);
let token = window.localStorage.getItem('token');
return fetch('https://bkcuvue.test/api/v1/artikel/upload', {
headers: {"Authorization": 'Bearer ' + token},
body: body,
method: 'POST'
})
.then(response => response.json())
.then(downloadUrl => {
return {
default: downloadUrl
}
})
.catch(error => {
console.log(error);
});
}
this.abort = () => {
console.log('Abort upload.')
}
},
and then on my backend (laravel) is just like this
public function upload(Request $request)
{
if(!empty($request->gambar))
$fileName = Helper::image_processing($this->imagepath,$this->width,$this->height,$request,'');
else
$fileName = '';
return response()->json('/' . $this->imagepath . $fileName . '.jpg');
}
to the ckeditor component
<vue-ckeditor type="classic" v-model="htmlContent" :upload-adapter="UploadAdapter"></vue-ckeditor>
and it works! but the problem that soon i realize is because this method is uploading the image to the server and then returning image path to or editor which will be saved to the database as a plain html tags...
then if for example i upload image and then i want to remove or maybe change the image then the editor will delete the img tag from the editor but not the real image file on the server....
the image file will remain on the server and there is no way i can delete it unless i select the image and delete it myself by going into server by ftp or ssh manualy.
is there any way to check if the image tag is deleted then it will delete on the server. Or maybe there is a way in backend to check if this image file is not used by any article...
and also if i upload the same image twice then it will also uploading to the server twice the same file, how can i detect if the same image file is already on the server and not upload duplicate but immediately return the image path