Issue #1418Opened September 11, 2018by ageir0 reactions

[BUG?] Asset manager not deleting files on server unless upload triggered.

Question

Running latest version (0.14.29) on local server. Using the example webpage preset. Ubuntu 18.04, Chromium 68.

Assetmanager init:

		assetManager: {
			storageType: '',
			storeOnChange: true,
			storeAfterUpload: true,
			embedAsBase64: 1,
			assets: all_files,
			noAssets: 'No assets here, drag to upload!',
			uploadFile: function (e) {
				var files = e.dataTransfer ? e.dataTransfer.files : e.target.files;
				var formData = new FormData();
				for (var i in files) {
					formData.append('file-' + i, files[i]); 
					console.log('File (i): ' + i + ' Files: ' + files);
				}
				$.ajax({
					url: '/file_upload',
					type: 'POST',
					data: formData,
					contentType: false,
					crossDomain: true,
					dataType: 'json',
					mimeType: "multipart/form-data",
					processData: false,
					success: function (result) {
						var images = result['data'];
						editor.AssetManager.add(images);
						console.log(images);
					}
				});
			},

			headers: {},
			params: {},
			autoAdd: 1,
			uploadText: 'Drop files here or click to upload.',
			addBtnText: 'Add image',
			dropzone: 0,
			openAssetsOnDrop: 0,
			dropzoneContent: '<div class="dropzone-inner">Drop media here.</div>',
			modalTitle: 'Select Image',
		},

And extend image type:

	const am = editor.AssetManager;
	editor.AssetManager.add(all_files);
	// Asset manager
	// Extend the original `image` and add a confirm dialog before removing it
	console.log('Adding custom image functions');
	am.addType('image', {
		// As you adding on top of an already defined type you can avoid indicating
		// `am.getType('image').view.extend({...` the editor will do it by default
		// but you can eventually extend some other type
		view: {
			// If you want to see more methods to extend check out
			// https://github.com/artf/grapesjs/blob/dev/src/asset_manager/view/AssetImageView.js
			onRemove(e) {
				e.stopPropagation();
				const model = this.model;
				console.log(model);
				console.log('ID:')
				console.log(model['attributes']['id'])
				if (confirm('Are you sure?')) {

					$.post('/file_delete/', {
						id: model['attributes']['id'],
					}, function (result) {
						if (result['success']) {
							console.log('Deleted file on server: ' + model['attributes']['id']);
							model.collection.remove(model);
						} else {
							model.collection.remove(model);
							console.log('Failed to delete file on server.');
						}
					}, 'json');
				}
			}
		},
	});

The all_files variable is load from php, and it has the right data and format, all images stored on the server.

The problem is that the onRemove is not triggering as it should. The images are showing up in the assetmanager modal correctly, but when pressing the X the function is not triggered. But they are deleted in the modal, but reappering when reloading the page. But if I upload a file, then it's working. The confirm popup is showing, and the file is deleted successfully on the server. Any Idea why this is happening? Did I miss anything or is this a bug?

Also when dragging an image to the canvas, the image is not showing up. Only the "missing file" ico and the name of the image.

Any help appreciated!

Answers (3)

artfSeptember 15, 20180 reactions

Hi Göran

but reappearing when reloading the page

you're initializing the asset manager with assets: all_files so if you don't change all_files you always get the same set of images

Also when dragging an image to the canvas, the image is not showing up. Only the "missing file" ico and the name of the image.

For this, I'd like to ask to create another issue and maybe also with the demo

ageirSeptember 17, 20180 reactions

@artf I don't think you understood me. They are reappearing because they didn't get deleted on the server. all_files are generated via php on page reload, and showing the correct files on the server.

The problem is that onRemove isn't triggered at all until I upload a file, then it is triggered. Why is that?

artfSeptember 17, 20180 reactions

Ah ok probably because your assets are already rendered. Try to apply those changes but inside a plugin

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.