blob: 6528b0dd765a2bc48880b269e8a2231479c379dc [file] [log] [blame]
2230100980aaf0d2025-06-05 23:20:05 +08001// export default PublishSeed;
2import React, { useState, useRef, useEffect } from 'react';
223010095b28c672025-04-10 20:12:45 +08003import axios from 'axios';
2230100964011632025-06-04 21:57:22 +08004import Header from '../../components/Header';
5import './PublishSeed.css';
6import { useUser } from '../../context/UserContext';
223010091e2aea72025-06-08 16:35:54 +08007import { uploadFile } from '../../api/file';
22301009207e2db2025-06-09 00:27:28 +08008import AuthButton from '../../components/AuthButton';
223010095b28c672025-04-10 20:12:45 +08009
10const PublishSeed = () => {
11 const [title, setTitle] = useState('');
12 const [description, setDescription] = useState('');
13 const [tags, setTags] = useState([]);
14 const [category, setCategory] = useState('movie');
223010095b28c672025-04-10 20:12:45 +080015 const [message, setMessage] = useState('');
16 const [isLoading, setIsLoading] = useState(false);
22301009df48f962025-06-05 13:40:44 +080017 const [fileName, setFileName] = useState('');
2230100980aaf0d2025-06-05 23:20:05 +080018 const [imageFile, setImageFile] = useState(null);
19 const [previewUrl, setPreviewUrl] = useState('');
223010095b28c672025-04-10 20:12:45 +080020
2230100980aaf0d2025-06-05 23:20:05 +080021 const fileInputRef = useRef(null);
22 const imageInputRef = useRef(null);
22301009df48f962025-06-05 13:40:44 +080023 const { user } = useUser();
2230100964011632025-06-04 21:57:22 +080024
2230100980aaf0d2025-06-05 23:20:05 +080025 useEffect(() => {
26 console.log('[DEBUG] 当前用户:', user);
27 }, [user]);
28
223010095b28c672025-04-10 20:12:45 +080029 const handleTagsChange = (e) => {
2230100980aaf0d2025-06-05 23:20:05 +080030 console.log('[DEBUG] 标签输入变化:', e.target.value);
31 setTags(e.target.value.split(',').map(tag => tag.trim()).filter(Boolean));
223010095b28c672025-04-10 20:12:45 +080032 };
33
22301009df48f962025-06-05 13:40:44 +080034 const handleFileButtonClick = () => {
2230100980aaf0d2025-06-05 23:20:05 +080035 console.log('[DEBUG] 触发文件选择按钮点击');
36 fileInputRef.current?.click();
37 };
38
39 const handleImageButtonClick = () => {
40 console.log('[DEBUG] 触发封面图片选择按钮点击');
41 imageInputRef.current?.click();
22301009df48f962025-06-05 13:40:44 +080042 };
43
223010095b28c672025-04-10 20:12:45 +080044 const handleFileChange = (e) => {
2230100980aaf0d2025-06-05 23:20:05 +080045 console.log('[DEBUG] 种子文件选择变化:', e.target.files);
22301009df48f962025-06-05 13:40:44 +080046 const selectedFile = e.target.files[0];
47 if (selectedFile) {
2230100980aaf0d2025-06-05 23:20:05 +080048 setFileName(selectedFile.name);
22301009df48f962025-06-05 13:40:44 +080049 }
223010095b28c672025-04-10 20:12:45 +080050 };
51
2230100980aaf0d2025-06-05 23:20:05 +080052 const handleImageChange = (e) => {
53 console.log('[DEBUG] 封面图片选择变化:', e.target.files);
54 const img = e.target.files[0];
55 if (!img) return;
56 setImageFile(img);
57 setPreviewUrl(URL.createObjectURL(img));
58 };
22301009df48f962025-06-05 13:40:44 +080059
2230100980aaf0d2025-06-05 23:20:05 +080060 const handleSubmit = async (e) => {
61 console.log('[DEBUG] handleSubmit 被触发', e);
62 e.preventDefault();
22301009df48f962025-06-05 13:40:44 +080063
223010095b28c672025-04-10 20:12:45 +080064 setIsLoading(true);
65 setMessage('');
66
2230100980aaf0d2025-06-05 23:20:05 +080067 const currentFile = fileInputRef.current?.files[0];
68 console.log('[DEBUG] 当前选择文件:', currentFile);
69
70 if (!user || !user.userId) {
71 console.log('[DEBUG] 用户未登录,阻止上传');
2230100964011632025-06-04 21:57:22 +080072 setMessage('请先登录');
73 setIsLoading(false);
74 return;
75 }
76
22301009df48f962025-06-05 13:40:44 +080077 if (!currentFile || !currentFile.name.toLowerCase().endsWith('.torrent')) {
2230100980aaf0d2025-06-05 23:20:05 +080078 console.log('[DEBUG] 文件校验失败');
2230100964011632025-06-04 21:57:22 +080079 setMessage('请上传一个 .torrent 文件');
80 setIsLoading(false);
81 return;
82 }
83
223010095b28c672025-04-10 20:12:45 +080084 const formData = new FormData();
22301009df48f962025-06-05 13:40:44 +080085 formData.append('file', currentFile);
223010095b28c672025-04-10 20:12:45 +080086 formData.append('title', title);
87 formData.append('description', description);
223010095b28c672025-04-10 20:12:45 +080088 formData.append('category', category);
2230100980aaf0d2025-06-05 23:20:05 +080089 formData.append('tags', tags.join(',')); // 逗号分隔字符串
90 formData.append('uploader', user.userId);
2230100980aaf0d2025-06-05 23:20:05 +080091 if (imageFile) {
2230100999cf3162025-06-06 00:19:25 +080092 formData.append('coverImage', imageFile);
2230100980aaf0d2025-06-05 23:20:05 +080093 }
223010095b28c672025-04-10 20:12:45 +080094
95 try {
2230100980aaf0d2025-06-05 23:20:05 +080096 console.log('[DEBUG] 发送上传请求...');
2230100964011632025-06-04 21:57:22 +080097 const response = await axios.post('/seeds/upload', formData, {
2230100980aaf0d2025-06-05 23:20:05 +080098 // axios 会自动处理 multipart/form-data Content-Type 边界,不用手动设置
99 // headers: { 'Content-Type': 'multipart/form-data' },
223010095b28c672025-04-10 20:12:45 +0800100 });
2230100980aaf0d2025-06-05 23:20:05 +0800101 console.log('[DEBUG] 请求成功,响应:', response.data);
223010095b28c672025-04-10 20:12:45 +0800102
22301009207e2db2025-06-09 00:27:28 +0800103 if (response.data.code === 0) {
223010095b28c672025-04-10 20:12:45 +0800104 setMessage('种子上传成功');
105 } else {
2230100980aaf0d2025-06-05 23:20:05 +0800106 setMessage(response.data.message || '上传失败,请稍后再试');
223010095b28c672025-04-10 20:12:45 +0800107 }
108 } catch (error) {
22301009df48f962025-06-05 13:40:44 +0800109 console.error('[handleSubmit] 上传失败:', error);
223010095b28c672025-04-10 20:12:45 +0800110 setMessage('上传失败,发生了错误');
111 } finally {
112 setIsLoading(false);
113 }
114 };
115
116 return (
223010091e2aea72025-06-08 16:35:54 +0800117 <div className="ps-container">
2230100964011632025-06-04 21:57:22 +0800118 <Header />
223010091e2aea72025-06-08 16:35:54 +0800119 <div className="ps-card">
120 {message && <div className="ps-message">{message}</div>}
2230100980aaf0d2025-06-05 23:20:05 +0800121 <form
223010091e2aea72025-06-08 16:35:54 +0800122 className="ps-form"
2230100980aaf0d2025-06-05 23:20:05 +0800123 onSubmit={(e) => {
124 console.log('[DEBUG] form onSubmit 触发');
125 handleSubmit(e);
126 }}
127 encType="multipart/form-data"
128 >
223010091e2aea72025-06-08 16:35:54 +0800129 <div>
130 <label className="ps-label">标题</label>
2230100964011632025-06-04 21:57:22 +0800131 <input
223010091e2aea72025-06-08 16:35:54 +0800132 className="ps-input-text"
2230100964011632025-06-04 21:57:22 +0800133 type="text"
134 value={title}
2230100980aaf0d2025-06-05 23:20:05 +0800135 onChange={(e) => {
136 console.log('[DEBUG] 标题输入变化:', e.target.value);
137 setTitle(e.target.value);
138 }}
2230100964011632025-06-04 21:57:22 +0800139 required
140 />
141 </div>
223010095b28c672025-04-10 20:12:45 +0800142
223010091e2aea72025-06-08 16:35:54 +0800143 <div>
144 <label className="ps-label">描述</label>
2230100964011632025-06-04 21:57:22 +0800145 <textarea
223010091e2aea72025-06-08 16:35:54 +0800146 className="ps-textarea"
2230100964011632025-06-04 21:57:22 +0800147 value={description}
2230100980aaf0d2025-06-05 23:20:05 +0800148 onChange={(e) => {
149 console.log('[DEBUG] 描述输入变化:', e.target.value);
150 setDescription(e.target.value);
151 }}
2230100964011632025-06-04 21:57:22 +0800152 required
153 />
154 </div>
223010095b28c672025-04-10 20:12:45 +0800155
223010091e2aea72025-06-08 16:35:54 +0800156 <div>
157 <label className="ps-label">标签 (逗号分隔)</label>
2230100964011632025-06-04 21:57:22 +0800158 <input
223010091e2aea72025-06-08 16:35:54 +0800159 className="ps-input-text"
2230100964011632025-06-04 21:57:22 +0800160 type="text"
161 value={tags.join(', ')}
162 onChange={handleTagsChange}
163 placeholder="例如:科幻, 动作"
164 required
165 />
166 </div>
223010095b28c672025-04-10 20:12:45 +0800167
223010091e2aea72025-06-08 16:35:54 +0800168 <div>
169 <label className="ps-label">分类</label>
2230100964011632025-06-04 21:57:22 +0800170 <select
223010091e2aea72025-06-08 16:35:54 +0800171 className="ps-select"
2230100964011632025-06-04 21:57:22 +0800172 value={category}
2230100980aaf0d2025-06-05 23:20:05 +0800173 onChange={(e) => {
174 console.log('[DEBUG] 分类选择变化:', e.target.value);
175 setCategory(e.target.value);
176 }}
2230100964011632025-06-04 21:57:22 +0800177 required
178 >
179 <option value="movie">电影</option>
180 <option value="tv">电视剧</option>
181 <option value="music">音乐</option>
22301111a289e262025-06-07 22:38:46 +0800182 <option value="anime">动漫</option>
183 <option value="game">游戏</option>
184 <option value="variety">综艺</option>
185 <option value="software">软件</option>
186 <option value="sports">体育</option>
187 <option value="study">学习</option>
188 <option value="documentary">纪录片</option>
189 <option value="other">其他</option>
2230100964011632025-06-04 21:57:22 +0800190 </select>
191 </div>
223010095b28c672025-04-10 20:12:45 +0800192
223010091e2aea72025-06-08 16:35:54 +0800193 <div>
194 <label className="ps-label">种子文件</label>
2230100980aaf0d2025-06-05 23:20:05 +0800195 <div
223010091e2aea72025-06-08 16:35:54 +0800196 className="ps-seed-file-label"
2230100980aaf0d2025-06-05 23:20:05 +0800197 onClick={handleFileButtonClick}
198 style={{ cursor: 'pointer' }}
199 >
Krishyaf1d0ea82025-05-03 17:01:58 +0800200 点击选择文件
22301009df48f962025-06-05 13:40:44 +0800201 </div>
202 <input
203 type="file"
204 accept=".torrent"
205 ref={fileInputRef}
206 onChange={handleFileChange}
223010091e2aea72025-06-08 16:35:54 +0800207 className="ps-seed-file-input"
22301009df48f962025-06-05 13:40:44 +0800208 />
223010091e2aea72025-06-08 16:35:54 +0800209 {fileName && <div style={{ marginTop: '5px', color: '#5F4437' }}>{fileName}</div>}
2230100964011632025-06-04 21:57:22 +0800210 </div>
223010095b28c672025-04-10 20:12:45 +0800211
223010091e2aea72025-06-08 16:35:54 +0800212 <div>
213 <label className="ps-label">封面图</label>
214 <div>
215 <button
216 type="button"
217 onClick={handleImageButtonClick}
218 className="ps-cover-upload-button"
219 >
2230100980aaf0d2025-06-05 23:20:05 +0800220 上传图片
221 </button>
222 <input
223 type="file"
224 accept="image/*"
225 ref={imageInputRef}
226 onChange={handleImageChange}
223010091e2aea72025-06-08 16:35:54 +0800227 className="ps-cover-upload-input"
2230100980aaf0d2025-06-05 23:20:05 +0800228 />
229 </div>
230 {previewUrl && (
231 <div style={{ marginTop: '10px' }}>
232 <img
233 src={previewUrl}
234 alt="封面预览"
223010091e2aea72025-06-08 16:35:54 +0800235 className="ps-img-preview"
2230100980aaf0d2025-06-05 23:20:05 +0800236 />
237 </div>
238 )}
2230100964011632025-06-04 21:57:22 +0800239 </div>
223010095b28c672025-04-10 20:12:45 +0800240
223010091e2aea72025-06-08 16:35:54 +0800241 <div className="ps-upload-button">
22301009207e2db2025-06-09 00:27:28 +0800242 <AuthButton
243   roles={["cookie", "chocolate", "ice-cream"]}
2230100980aaf0d2025-06-05 23:20:05 +0800244 type="submit"
245 disabled={isLoading}
246 onClick={() => console.log('[DEBUG] 上传按钮 onClick 触发')}
247 >
2230100964011632025-06-04 21:57:22 +0800248 {isLoading ? '正在上传...' : '上传种子'}
22301009207e2db2025-06-09 00:27:28 +0800249 </AuthButton>
2230100964011632025-06-04 21:57:22 +0800250 </div>
251 </form>
Krishyac0f7e9b2025-04-22 15:28:28 +0800252 </div>
223010095b28c672025-04-10 20:12:45 +0800253 </div>
254 );
255};
256
257export default PublishSeed;