Skip to content

Commit 7c1dd18

Browse files
committed
image upload from torrent upload page.
front-end to back-end with upload system => OK back-end to db from api => OK
1 parent 2e94106 commit 7c1dd18

3 files changed

Lines changed: 60 additions & 1 deletion

File tree

api/src/controllers/torrent.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export const uploadTorrent = async (req, res, next) => {
137137
source: req.body.source,
138138
infoHash,
139139
binary: req.body.torrent,
140+
poster: req.body.poster,
140141
uploadedBy: req.userId,
141142
downloads: 0,
142143
anonymous: false,
@@ -154,7 +155,9 @@ export const uploadTorrent = async (req, res, next) => {
154155
group: groupId,
155156
mediaInfo: req.body.mediaInfo,
156157
});
157-
158+
console.log(req.body.name);
159+
console.log(req.body.poster);
160+
console.log(req.body);
158161
await newTorrent.save();
159162

160163
if (groupId) await addToGroup(groupId, infoHash);

api/src/schema/torrent.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fuzzySearch from "mongoose-fuzzy-searching";
44
const Torrent = new mongoose.Schema({
55
infoHash: String,
66
binary: String,
7+
poster: String,
78
uploadedBy: mongoose.Schema.ObjectId,
89
name: String,
910
description: String,

client/pages/upload.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ export const TorrentFields = ({
185185

186186
const Upload = ({ token, userId }) => {
187187
const [torrentFile, setTorrentFile] = useState();
188+
const [posterFile, setPosterFile] = useState();
188189
const [dropError, setDropError] = useState("");
189190
const [groupSuggestions, setGroupSuggestions] = useState([]);
190191

@@ -234,19 +235,51 @@ const Upload = ({ token, userId }) => {
234235
setDropError(e.message);
235236
}
236237
}, []);
238+
const onPosterDrop = useCallback((acceptedFiles) => {
239+
try {
240+
const [file] = acceptedFiles;
241+
if (file) {
242+
const reader = new FileReader();
243+
reader.onload = async () => {
244+
console.log(`[DEBUG] Poster upload complete: ${reader.result.slice(0, 64)}...`);
245+
const [, posterB64] = reader.result.split("base64,");
246+
setPosterFile({ name: file.name, b64: posterB64 });
247+
};
248+
reader.onerror = () => {
249+
console.log(`[DEBUG] Poster upload error: ${reader.error}`);
250+
// Gérer les erreurs, si nécessaire
251+
};
252+
reader.readAsDataURL(file);
253+
}
254+
} catch (e) {
255+
console.error(e);
256+
// Gérer les erreurs, si nécessaire
257+
}
258+
}, []);
259+
237260

238261
const { getRootProps, getInputProps, isDragActive } = useDropzone({
239262
onDrop,
240263
accept: { "application/x-bittorrent": [".torrent"] },
241264
maxFiles: 1,
242265
});
266+
const { getRootProps: getPosterRootProps, getInputProps: getPosterInputProps, isDragActive: isPosterDragActive } = useDropzone({
267+
onDrop: onPosterDrop,
268+
accept: {
269+
"image/jpeg": [".jpg", ".jpeg"],
270+
"image/png": [".png"],
271+
},
272+
maxFiles: 1,
273+
});
274+
243275

244276
const handleUpload = async (e) => {
245277
e.preventDefault();
246278
setLoading(true);
247279
const form = new FormData(e.target);
248280

249281
try {
282+
form.append("poster", posterFile.b64); // Ajoutez la valeur de l'image de l'affiche à la requête
250283
if (!torrentFile) throw new Error("No .torrent file added");
251284

252285
const uploadRes = await fetch(`${SQ_API_URL}/torrent/upload`, {
@@ -265,6 +298,7 @@ const Upload = ({ token, userId }) => {
265298
tags: form.get("tags"),
266299
groupWith,
267300
mediaInfo: form.get("mediaInfo"),
301+
poster: posterFile.b64,
268302
}),
269303
});
270304

@@ -381,6 +415,27 @@ const Upload = ({ token, userId }) => {
381415
</Text>
382416
)}
383417
</Box>
418+
<Box mb={4}>
419+
<WrapLabel label={getLocaleString("uploadPosterImage")} as="div">
420+
<FileUpload {...getPosterRootProps()}>
421+
<input {...getPosterInputProps()} />
422+
{posterFile ? (
423+
<Text icon={Check} iconColor="success" iconSize={24} ml={2}>
424+
{posterFile.name}
425+
</Text>
426+
) : isPosterDragActive ? (
427+
<Text color="grey">
428+
{getLocaleString("uploadDropImageHere")}
429+
</Text>
430+
) : (
431+
<Text color="grey">
432+
{getLocaleString("uploadDragDropClickSelect")}
433+
</Text>
434+
)}
435+
</FileUpload>
436+
</WrapLabel>
437+
</Box>
438+
384439
<TorrentFields
385440
categories={SQ_TORRENT_CATEGORIES}
386441
handleGroupSearch={handleGroupSearch}

0 commit comments

Comments
 (0)