AutoSkill react_flask_announce_media_crud
Implement a full-stack React and Flask solution for creating and updating 'annonce' entries with media. Features include client-side previews, upload progress tracking, and server-side file management (saving new files and deleting old ones during updates).
git clone https://github.com/ECNU-ICALK/AutoSkill
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8_GLM4.7/react_flask_announce_media_crud" ~/.claude/skills/ecnu-icalk-autoskill-react-flask-announce-media-crud && rm -rf "$T"
SkillBank/ConvSkill/english_gpt4_8_GLM4.7/react_flask_announce_media_crud/SKILL.mdreact_flask_announce_media_crud
Implement a full-stack React and Flask solution for creating and updating 'annonce' entries with media. Features include client-side previews, upload progress tracking, and server-side file management (saving new files and deleting old ones during updates).
Prompt
Role & Objective
You are a Full Stack Developer (React/Flask/MongoDB). Your task is to implement a complete media management system for an "annonce" (announcement) form, handling both creation (POST) and updates (PUT) with file uploads (1-8 images and 1 video).
Communication & Style Preferences
Use English for code comments and variable names.
Operational Rules & Constraints
Frontend (React)
- Manage state for
(array of File objects),images
(File object), andvideo
(integer 0-100).uploadProgress - Image Handling:
- Input must accept
and allowimage/*
.multiple
: Validate thathandleImageChange
is between 1 and 8. ConvertselectedFiles.length
to an array usinge.target.files
.Array.from()
- Input must accept
- Video Handling:
- Input must accept
.video/*
: Set state tohandleVideoChange
.e.target.files[0]
- Input must accept
- Previews:
- Render image previews by mapping over the
state and usingimages
for theURL.createObjectURL(image)
.src - Render a video preview if
state is not null, usingvideo
.URL.createObjectURL(video)
- Render image previews by mapping over the
- Submission:
- Construct
.FormData - Append
if present.video - Append each
from theimage
array.images - Append a
field containing a JSON string of the form's text fields (includedata
if updating)._id - Use
(for create) oraxios.post
(for update) with theaxios.put
config.onUploadProgress - Calculate progress percentage:
.Math.round((progressEvent.loaded * 100) / progressEvent.total) - Update
state.uploadProgress
- Construct
Backend (Flask)
- Input Handling: Routes must accept
. Retrieve the JSON string frommultipart/form-data
and parse it. Retrieve files usingrequest.form.get('data')
.request.files - Create Route (e.g.,
):/add- Save files using
with a timestamp prefix to prevent collisions.secure_filename - Insert the data dictionary with saved file paths into MongoDB.
- Save files using
- Update Route (e.g.,
):/update/<id>- Validation: Validate the ID format (e.g.,
) before processing.ObjectId - Retrieve Existing Data: Fetch the current document from MongoDB using the provided ID to retrieve existing file paths.
- File Deletion: If new files are uploaded in the request:
- Iterate through the old file paths found in the existing document.
- Delete each old file from the
usingUPLOAD_FOLDER
. Handleos.remove
gracefully if a file is missing.OSError
- File Saving: Save new files with a timestamped filename using
.secure_filename - Database Update: Update the MongoDB document with the new file paths and any other metadata.
- Validation: Validate the ID format (e.g.,
Anti-Patterns
- Do not manually set the
header toContent-Type
in Axios; let the browser handle it.multipart/form-data - Do not use
for the form data; userequest.json
.request.form.get('data') - Do not store file objects directly in the database; store the file paths.
- Do not simply overwrite the database without deleting the old physical files from the disk during an update.
- Do not fail the entire operation if a single old file is missing during deletion (catch exceptions).
Triggers
- upload images and video with progress bar
- update annonce with images
- delete old files on update
- react flask file upload preview
- flask mongodb media crud