M
M
Mashush2022-04-01 09:01:03
JavaScript
Mashush, 2022-04-01 09:01:03

How to implement file upload when creating and editing a post?

And so, I am relatively beginner, so this question arose. There is a user who can create and edit certain entries. The post itself consists of a title, a description, and a few photos.

When sending a request to create an entry, it executes middleware based on the multer package, which saves the transferred photos. The record is stored in the database with the fields of the name, description, user ID and an array of paths to the transferred photos.

And here is the problem - how, for example, to implement editing of this record? When editing, the user can leave the same photos and fix something else, or he can completely remove old photos and replace them with new ones, or replace only a couple of photos.

We need to implement functionality that would:

1. Did not add those photos when editing that were already saved when creating
2. Deleted those photos when editing that were already saved, but the user decided to delete them

everyone thanks for any information

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kyber_Ded, 2022-04-01
@Kyber_Ded

When editing, store an array of old paths to photos, and when filling, compare what has already changed. If from the second, new, found a new path, then add, if not found in the new old path - delete. Those that have not undergone change, do not touch

A
Alexander, 2022-04-01
@anudas

1. It probably depends on which post editor is used on the front. in principle, this question is more about the front. it needs a button that will upload the photo and insert the desired code into the text of the post. For example bb-code or markdown. When you save a post, the photos will neither be uploaded nor deleted. loading occurs only upon loading through the corresponding button
2. Perform regular garbage collection. Go through the texts of all posts, collect links to files or pictures and delete files that do not have links

T
ThunderCat, 2022-04-01
@ThunderCat

The record is stored in the database with the fields of the name, description, user ID and an array of paths to the transferred photos.
Firstly, an array of paths to the transferred photos - is it a field with a json? Beautiful, magical, convenient, what can I say ... Secondly - 3rd normal form, because it was not in vain that they invented it.
It is necessary to implement functionality that would:
1. Do not add those photos when editing that were already saved when creating
First - why? I want 5 identical asses, why not? Secondly - to determine that this is the same file - how? By the name? That is, I download the 1.jpeg file, and then from another place also 1.jpeg, a completely different file, ich? What to do? I mean, I know what to do, I just give a hint that since you don’t need to do it. About the fact that conditional Vasya can also upload 1.zhpeg, and if they are all stored in the same folder, instead of my ass there will be someone else's ...
2. Deleted those photos during editing that were already saved, but the user decided to delete them
I don’t see a problem at all, well, in the sense that there are options in the current implementation of this trash, but they are all miserable, such as passing the path to the file with Ajax, since the files you don’t have IDs in the database, pass the record IDs there so that you don’t sort through everything , then check if there is such a path in this entry, then delete it from the array, delete the file, wrap the array back into a json and write it back.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question