@quantabit/media-sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +22 -0
- package/dist/index.cjs +790 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.esm.js +783 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/styles.css +2 -0
- package/dist/styles.css.map +1 -0
- package/package.json +71 -0
- package/types/index.d.ts +5 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,790 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
var sdkConfig = require('@quantabit/sdk-config');
|
|
5
|
+
|
|
6
|
+
function ImageViewer({
|
|
7
|
+
src,
|
|
8
|
+
alt = '',
|
|
9
|
+
zoom = true,
|
|
10
|
+
className = ''
|
|
11
|
+
}) {
|
|
12
|
+
const [fullscreen, setFullscreen] = React.useState(false);
|
|
13
|
+
const [scale, setScale] = React.useState(1);
|
|
14
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("img", {
|
|
15
|
+
src: src,
|
|
16
|
+
alt: alt,
|
|
17
|
+
onClick: () => zoom && setFullscreen(true),
|
|
18
|
+
className: `qmd-img ${className}`,
|
|
19
|
+
style: {
|
|
20
|
+
maxWidth: '100%',
|
|
21
|
+
borderRadius: 8,
|
|
22
|
+
cursor: zoom ? 'zoom-in' : 'default',
|
|
23
|
+
transition: 'all 0.2s'
|
|
24
|
+
}
|
|
25
|
+
}), fullscreen && /*#__PURE__*/React.createElement("div", {
|
|
26
|
+
onClick: () => {
|
|
27
|
+
setFullscreen(false);
|
|
28
|
+
setScale(1);
|
|
29
|
+
},
|
|
30
|
+
style: {
|
|
31
|
+
position: 'fixed',
|
|
32
|
+
inset: 0,
|
|
33
|
+
zIndex: 99999,
|
|
34
|
+
background: 'rgba(0,0,0,0.9)',
|
|
35
|
+
display: 'flex',
|
|
36
|
+
alignItems: 'center',
|
|
37
|
+
justifyContent: 'center',
|
|
38
|
+
cursor: 'zoom-out'
|
|
39
|
+
}
|
|
40
|
+
}, /*#__PURE__*/React.createElement("img", {
|
|
41
|
+
src: src,
|
|
42
|
+
alt: alt,
|
|
43
|
+
style: {
|
|
44
|
+
maxWidth: '90vw',
|
|
45
|
+
maxHeight: '90vh',
|
|
46
|
+
objectFit: 'contain',
|
|
47
|
+
transform: `scale(${scale})`,
|
|
48
|
+
transition: 'transform 0.2s'
|
|
49
|
+
},
|
|
50
|
+
onClick: e => {
|
|
51
|
+
e.stopPropagation();
|
|
52
|
+
setScale(s => s >= 2 ? 1 : s + 0.5);
|
|
53
|
+
}
|
|
54
|
+
}), /*#__PURE__*/React.createElement("button", {
|
|
55
|
+
onClick: () => {
|
|
56
|
+
setFullscreen(false);
|
|
57
|
+
setScale(1);
|
|
58
|
+
},
|
|
59
|
+
style: {
|
|
60
|
+
position: 'absolute',
|
|
61
|
+
top: 20,
|
|
62
|
+
right: 20,
|
|
63
|
+
border: 'none',
|
|
64
|
+
background: 'rgba(255,255,255,0.1)',
|
|
65
|
+
color: '#fff',
|
|
66
|
+
width: 36,
|
|
67
|
+
height: 36,
|
|
68
|
+
borderRadius: '50%',
|
|
69
|
+
cursor: 'pointer',
|
|
70
|
+
fontSize: 18
|
|
71
|
+
}
|
|
72
|
+
}, "\u2715")));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function VideoPlayer({
|
|
76
|
+
src,
|
|
77
|
+
poster,
|
|
78
|
+
autoplay = false,
|
|
79
|
+
controls = true,
|
|
80
|
+
width = '100%',
|
|
81
|
+
aspectRatio = '16/9',
|
|
82
|
+
className = ''
|
|
83
|
+
}) {
|
|
84
|
+
const ref = React.useRef(null);
|
|
85
|
+
const [playing, setPlaying] = React.useState(autoplay);
|
|
86
|
+
const toggle = () => {
|
|
87
|
+
if (ref.current) {
|
|
88
|
+
if (ref.current.paused) {
|
|
89
|
+
ref.current.play();
|
|
90
|
+
setPlaying(true);
|
|
91
|
+
} else {
|
|
92
|
+
ref.current.pause();
|
|
93
|
+
setPlaying(false);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
98
|
+
className: `qmd-video ${className}`,
|
|
99
|
+
style: {
|
|
100
|
+
position: 'relative',
|
|
101
|
+
width,
|
|
102
|
+
aspectRatio,
|
|
103
|
+
borderRadius: 12,
|
|
104
|
+
overflow: 'hidden',
|
|
105
|
+
background: '#000'
|
|
106
|
+
}
|
|
107
|
+
}, /*#__PURE__*/React.createElement("video", {
|
|
108
|
+
ref: ref,
|
|
109
|
+
src: src,
|
|
110
|
+
poster: poster,
|
|
111
|
+
autoPlay: autoplay,
|
|
112
|
+
controls: controls,
|
|
113
|
+
playsInline: true,
|
|
114
|
+
style: {
|
|
115
|
+
width: '100%',
|
|
116
|
+
height: '100%',
|
|
117
|
+
objectFit: 'cover'
|
|
118
|
+
},
|
|
119
|
+
onPlay: () => setPlaying(true),
|
|
120
|
+
onPause: () => setPlaying(false)
|
|
121
|
+
}), !controls && /*#__PURE__*/React.createElement("button", {
|
|
122
|
+
onClick: toggle,
|
|
123
|
+
style: {
|
|
124
|
+
position: 'absolute',
|
|
125
|
+
inset: 0,
|
|
126
|
+
border: 'none',
|
|
127
|
+
background: 'transparent',
|
|
128
|
+
cursor: 'pointer',
|
|
129
|
+
display: 'flex',
|
|
130
|
+
alignItems: 'center',
|
|
131
|
+
justifyContent: 'center'
|
|
132
|
+
}
|
|
133
|
+
}, !playing && /*#__PURE__*/React.createElement("div", {
|
|
134
|
+
style: {
|
|
135
|
+
width: 60,
|
|
136
|
+
height: 60,
|
|
137
|
+
borderRadius: '50%',
|
|
138
|
+
background: 'rgba(0,0,0,0.6)',
|
|
139
|
+
display: 'flex',
|
|
140
|
+
alignItems: 'center',
|
|
141
|
+
justifyContent: 'center'
|
|
142
|
+
}
|
|
143
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
144
|
+
style: {
|
|
145
|
+
width: 0,
|
|
146
|
+
height: 0,
|
|
147
|
+
borderLeft: '18px solid #fff',
|
|
148
|
+
borderTop: '12px solid transparent',
|
|
149
|
+
borderBottom: '12px solid transparent',
|
|
150
|
+
marginLeft: 4
|
|
151
|
+
}
|
|
152
|
+
}))));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function AudioPlayer({
|
|
156
|
+
src,
|
|
157
|
+
title,
|
|
158
|
+
artist,
|
|
159
|
+
cover,
|
|
160
|
+
className = ''
|
|
161
|
+
}) {
|
|
162
|
+
const ref = React.useRef(null);
|
|
163
|
+
const [playing, setPlaying] = React.useState(false);
|
|
164
|
+
const [progress, setProgress] = React.useState(0);
|
|
165
|
+
const [duration, setDuration] = React.useState(0);
|
|
166
|
+
const toggle = () => {
|
|
167
|
+
if (ref.current) {
|
|
168
|
+
if (playing) ref.current.pause();else ref.current.play();
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
const fmt = s => {
|
|
172
|
+
const m = Math.floor(s / 60);
|
|
173
|
+
return m + ':' + String(Math.floor(s % 60)).padStart(2, '0');
|
|
174
|
+
};
|
|
175
|
+
React.useEffect(() => {
|
|
176
|
+
const a = ref.current;
|
|
177
|
+
if (!a) return;
|
|
178
|
+
const onTime = () => setProgress(a.currentTime);
|
|
179
|
+
const onMeta = () => setDuration(a.duration);
|
|
180
|
+
a.addEventListener('timeupdate', onTime);
|
|
181
|
+
a.addEventListener('loadedmetadata', onMeta);
|
|
182
|
+
return () => {
|
|
183
|
+
a.removeEventListener('timeupdate', onTime);
|
|
184
|
+
a.removeEventListener('loadedmetadata', onMeta);
|
|
185
|
+
};
|
|
186
|
+
}, []);
|
|
187
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
188
|
+
className: `qmd-audio ${className}`,
|
|
189
|
+
style: {
|
|
190
|
+
display: 'flex',
|
|
191
|
+
alignItems: 'center',
|
|
192
|
+
gap: 12,
|
|
193
|
+
padding: '12px 16px',
|
|
194
|
+
borderRadius: 14,
|
|
195
|
+
border: '1px solid #e4e4e7',
|
|
196
|
+
background: '#fff'
|
|
197
|
+
}
|
|
198
|
+
}, /*#__PURE__*/React.createElement("audio", {
|
|
199
|
+
ref: ref,
|
|
200
|
+
src: src,
|
|
201
|
+
onPlay: () => setPlaying(true),
|
|
202
|
+
onPause: () => setPlaying(false)
|
|
203
|
+
}), cover && /*#__PURE__*/React.createElement("img", {
|
|
204
|
+
src: cover,
|
|
205
|
+
alt: "",
|
|
206
|
+
style: {
|
|
207
|
+
width: 44,
|
|
208
|
+
height: 44,
|
|
209
|
+
borderRadius: 8,
|
|
210
|
+
objectFit: 'cover',
|
|
211
|
+
flexShrink: 0
|
|
212
|
+
}
|
|
213
|
+
}), /*#__PURE__*/React.createElement("div", {
|
|
214
|
+
style: {
|
|
215
|
+
flex: 1
|
|
216
|
+
}
|
|
217
|
+
}, title && /*#__PURE__*/React.createElement("div", {
|
|
218
|
+
style: {
|
|
219
|
+
fontSize: 13,
|
|
220
|
+
fontWeight: 600,
|
|
221
|
+
color: '#18181b'
|
|
222
|
+
}
|
|
223
|
+
}, title), artist && /*#__PURE__*/React.createElement("div", {
|
|
224
|
+
style: {
|
|
225
|
+
fontSize: 11,
|
|
226
|
+
color: '#71717a'
|
|
227
|
+
}
|
|
228
|
+
}, artist), /*#__PURE__*/React.createElement("div", {
|
|
229
|
+
style: {
|
|
230
|
+
display: 'flex',
|
|
231
|
+
alignItems: 'center',
|
|
232
|
+
gap: 6,
|
|
233
|
+
marginTop: 4
|
|
234
|
+
}
|
|
235
|
+
}, /*#__PURE__*/React.createElement("span", {
|
|
236
|
+
style: {
|
|
237
|
+
fontSize: 10,
|
|
238
|
+
color: '#a1a1aa',
|
|
239
|
+
width: 30
|
|
240
|
+
}
|
|
241
|
+
}, fmt(progress)), /*#__PURE__*/React.createElement("div", {
|
|
242
|
+
onClick: e => {
|
|
243
|
+
const r = e.currentTarget.getBoundingClientRect();
|
|
244
|
+
const pct = (e.clientX - r.left) / r.width;
|
|
245
|
+
ref.current.currentTime = pct * duration;
|
|
246
|
+
},
|
|
247
|
+
style: {
|
|
248
|
+
flex: 1,
|
|
249
|
+
height: 4,
|
|
250
|
+
borderRadius: 2,
|
|
251
|
+
background: '#e4e4e7',
|
|
252
|
+
cursor: 'pointer',
|
|
253
|
+
position: 'relative'
|
|
254
|
+
}
|
|
255
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
256
|
+
style: {
|
|
257
|
+
height: '100%',
|
|
258
|
+
width: duration ? `${progress / duration * 100}%` : '0%',
|
|
259
|
+
background: '#3b82f6',
|
|
260
|
+
borderRadius: 2,
|
|
261
|
+
transition: 'width 0.1s'
|
|
262
|
+
}
|
|
263
|
+
})), /*#__PURE__*/React.createElement("span", {
|
|
264
|
+
style: {
|
|
265
|
+
fontSize: 10,
|
|
266
|
+
color: '#a1a1aa',
|
|
267
|
+
width: 30
|
|
268
|
+
}
|
|
269
|
+
}, fmt(duration)))), /*#__PURE__*/React.createElement("button", {
|
|
270
|
+
onClick: toggle,
|
|
271
|
+
style: {
|
|
272
|
+
width: 36,
|
|
273
|
+
height: 36,
|
|
274
|
+
borderRadius: '50%',
|
|
275
|
+
border: 'none',
|
|
276
|
+
background: '#3b82f6',
|
|
277
|
+
color: '#fff',
|
|
278
|
+
cursor: 'pointer',
|
|
279
|
+
display: 'flex',
|
|
280
|
+
alignItems: 'center',
|
|
281
|
+
justifyContent: 'center',
|
|
282
|
+
flexShrink: 0
|
|
283
|
+
}
|
|
284
|
+
}, playing ? '⏸' : '▶'));
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function Gallery({
|
|
288
|
+
images = [],
|
|
289
|
+
columns = 3,
|
|
290
|
+
gap = 8,
|
|
291
|
+
className = ''
|
|
292
|
+
}) {
|
|
293
|
+
const [active, setActive] = React.useState(null);
|
|
294
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
|
|
295
|
+
className: `qmd-gallery ${className}`,
|
|
296
|
+
style: {
|
|
297
|
+
display: 'grid',
|
|
298
|
+
gridTemplateColumns: `repeat(${columns},1fr)`,
|
|
299
|
+
gap
|
|
300
|
+
}
|
|
301
|
+
}, images.map((img, i) => {
|
|
302
|
+
const src = typeof img === 'string' ? img : img.src;
|
|
303
|
+
const alt = img.alt || '';
|
|
304
|
+
return /*#__PURE__*/React.createElement("img", {
|
|
305
|
+
key: i,
|
|
306
|
+
src: src,
|
|
307
|
+
alt: alt,
|
|
308
|
+
onClick: () => setActive(i),
|
|
309
|
+
style: {
|
|
310
|
+
width: '100%',
|
|
311
|
+
aspectRatio: '1',
|
|
312
|
+
objectFit: 'cover',
|
|
313
|
+
borderRadius: 8,
|
|
314
|
+
cursor: 'pointer',
|
|
315
|
+
transition: 'all 0.2s'
|
|
316
|
+
},
|
|
317
|
+
className: "qmd-gallery-item"
|
|
318
|
+
});
|
|
319
|
+
})), active != null && /*#__PURE__*/React.createElement("div", {
|
|
320
|
+
onClick: () => setActive(null),
|
|
321
|
+
style: {
|
|
322
|
+
position: 'fixed',
|
|
323
|
+
inset: 0,
|
|
324
|
+
zIndex: 99999,
|
|
325
|
+
background: 'rgba(0,0,0,0.9)',
|
|
326
|
+
display: 'flex',
|
|
327
|
+
alignItems: 'center',
|
|
328
|
+
justifyContent: 'center'
|
|
329
|
+
}
|
|
330
|
+
}, /*#__PURE__*/React.createElement("button", {
|
|
331
|
+
onClick: e => {
|
|
332
|
+
e.stopPropagation();
|
|
333
|
+
setActive(a => a > 0 ? a - 1 : images.length - 1);
|
|
334
|
+
},
|
|
335
|
+
style: {
|
|
336
|
+
position: 'absolute',
|
|
337
|
+
left: 20,
|
|
338
|
+
border: 'none',
|
|
339
|
+
background: 'rgba(255,255,255,0.1)',
|
|
340
|
+
color: '#fff',
|
|
341
|
+
width: 40,
|
|
342
|
+
height: 40,
|
|
343
|
+
borderRadius: '50%',
|
|
344
|
+
cursor: 'pointer',
|
|
345
|
+
fontSize: 18
|
|
346
|
+
}
|
|
347
|
+
}, "\u2039"), /*#__PURE__*/React.createElement("img", {
|
|
348
|
+
src: typeof images[active] === 'string' ? images[active] : images[active].src,
|
|
349
|
+
alt: "",
|
|
350
|
+
style: {
|
|
351
|
+
maxWidth: '85vw',
|
|
352
|
+
maxHeight: '85vh',
|
|
353
|
+
objectFit: 'contain',
|
|
354
|
+
borderRadius: 8
|
|
355
|
+
}
|
|
356
|
+
}), /*#__PURE__*/React.createElement("button", {
|
|
357
|
+
onClick: e => {
|
|
358
|
+
e.stopPropagation();
|
|
359
|
+
setActive(a => (a + 1) % images.length);
|
|
360
|
+
},
|
|
361
|
+
style: {
|
|
362
|
+
position: 'absolute',
|
|
363
|
+
right: 20,
|
|
364
|
+
border: 'none',
|
|
365
|
+
background: 'rgba(255,255,255,0.1)',
|
|
366
|
+
color: '#fff',
|
|
367
|
+
width: 40,
|
|
368
|
+
height: 40,
|
|
369
|
+
borderRadius: '50%',
|
|
370
|
+
cursor: 'pointer',
|
|
371
|
+
fontSize: 18
|
|
372
|
+
}
|
|
373
|
+
}, "\u203A"), /*#__PURE__*/React.createElement("button", {
|
|
374
|
+
onClick: () => setActive(null),
|
|
375
|
+
style: {
|
|
376
|
+
position: 'absolute',
|
|
377
|
+
top: 20,
|
|
378
|
+
right: 20,
|
|
379
|
+
border: 'none',
|
|
380
|
+
background: 'rgba(255,255,255,0.1)',
|
|
381
|
+
color: '#fff',
|
|
382
|
+
width: 36,
|
|
383
|
+
height: 36,
|
|
384
|
+
borderRadius: '50%',
|
|
385
|
+
cursor: 'pointer',
|
|
386
|
+
fontSize: 18
|
|
387
|
+
}
|
|
388
|
+
}, "\u2715"), /*#__PURE__*/React.createElement("div", {
|
|
389
|
+
style: {
|
|
390
|
+
position: 'absolute',
|
|
391
|
+
bottom: 20,
|
|
392
|
+
color: '#fff',
|
|
393
|
+
fontSize: 13
|
|
394
|
+
}
|
|
395
|
+
}, active + 1, " / ", images.length)));
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Media SDK - API 客户端
|
|
400
|
+
* 媒体服务后端接口封装
|
|
401
|
+
*
|
|
402
|
+
* 使用 BaseApiClient 基类简化代码
|
|
403
|
+
*/
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* 媒体 API 客户端
|
|
408
|
+
*/
|
|
409
|
+
class MediaApiClient extends sdkConfig.BaseApiClient {
|
|
410
|
+
constructor(config = {}) {
|
|
411
|
+
super('/media', config);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// ============ 上传 ============
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* 获取上传凭证
|
|
418
|
+
* @param {Object} options - 上传选项
|
|
419
|
+
*/
|
|
420
|
+
async getUploadToken(options = {}) {
|
|
421
|
+
return this.post('/upload/token', options);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* 上传图片
|
|
426
|
+
* @param {File} file - 文件对象
|
|
427
|
+
* @param {Object} options - 上传选项
|
|
428
|
+
* @param {Function} onProgress - 进度回调
|
|
429
|
+
*/
|
|
430
|
+
async uploadImage(file, options = {}, onProgress = null) {
|
|
431
|
+
return this._upload('/upload/image', file, options, onProgress);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* 上传视频
|
|
436
|
+
* @param {File} file - 文件对象
|
|
437
|
+
* @param {Object} options - 上传选项
|
|
438
|
+
* @param {Function} onProgress - 进度回调
|
|
439
|
+
*/
|
|
440
|
+
async uploadVideo(file, options = {}, onProgress = null) {
|
|
441
|
+
return this._upload('/upload/video', file, options, onProgress);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* 上传音频
|
|
446
|
+
* @param {File} file - 文件对象
|
|
447
|
+
* @param {Object} options - 上传选项
|
|
448
|
+
* @param {Function} onProgress - 进度回调
|
|
449
|
+
*/
|
|
450
|
+
async uploadAudio(file, options = {}, onProgress = null) {
|
|
451
|
+
return this._upload('/upload/audio', file, options, onProgress);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* 内部上传方法
|
|
456
|
+
*/
|
|
457
|
+
async _upload(path, file, options, onProgress) {
|
|
458
|
+
const formData = new FormData();
|
|
459
|
+
formData.append('file', file);
|
|
460
|
+
Object.entries(options).forEach(([key, value]) => {
|
|
461
|
+
formData.append(key, value);
|
|
462
|
+
});
|
|
463
|
+
return new Promise((resolve, reject) => {
|
|
464
|
+
const xhr = new XMLHttpRequest();
|
|
465
|
+
const token = this.getToken();
|
|
466
|
+
if (onProgress) {
|
|
467
|
+
xhr.upload.onprogress = e => {
|
|
468
|
+
if (e.lengthComputable) {
|
|
469
|
+
onProgress(e.loaded / e.total);
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
xhr.onload = () => {
|
|
474
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
475
|
+
resolve(JSON.parse(xhr.responseText));
|
|
476
|
+
} else {
|
|
477
|
+
reject(new Error(`Upload failed: ${xhr.status}`));
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
xhr.onerror = () => reject(new Error('Upload failed'));
|
|
481
|
+
xhr.open('POST', `${this.baseUrl}${path}`);
|
|
482
|
+
if (token) xhr.setRequestHeader('Authorization', `Bearer ${token}`);
|
|
483
|
+
xhr.send(formData);
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// ============ 媒体查询 ============
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* 获取媒体列表
|
|
491
|
+
* @param {Object} params - 查询参数
|
|
492
|
+
*/
|
|
493
|
+
async getMediaList(params = {}) {
|
|
494
|
+
return this.get('/list', params);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* 获取媒体详情
|
|
499
|
+
* @param {string} mediaId - 媒体 ID
|
|
500
|
+
*/
|
|
501
|
+
async getMedia(mediaId) {
|
|
502
|
+
return this.get(`/${mediaId}`);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* 删除媒体
|
|
507
|
+
* @param {string} mediaId - 媒体 ID
|
|
508
|
+
*/
|
|
509
|
+
async deleteMedia(mediaId) {
|
|
510
|
+
return this.delete(`/${mediaId}`);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* 批量删除媒体
|
|
515
|
+
* @param {string[]} mediaIds - 媒体 ID 列表
|
|
516
|
+
*/
|
|
517
|
+
async batchDeleteMedia(mediaIds) {
|
|
518
|
+
return this.post('/batch-delete', {
|
|
519
|
+
media_ids: mediaIds
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// ============ 图片处理 ============
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* 获取图片缩略图 URL
|
|
527
|
+
* @param {string} mediaId - 媒体 ID
|
|
528
|
+
* @param {Object} options - 处理选项
|
|
529
|
+
*/
|
|
530
|
+
async getThumbnailUrl(mediaId, options = {}) {
|
|
531
|
+
return this.get(`/${mediaId}/thumbnail`, options);
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* 裁剪图片
|
|
536
|
+
* @param {string} mediaId - 媒体 ID
|
|
537
|
+
* @param {Object} cropOptions - 裁剪参数
|
|
538
|
+
*/
|
|
539
|
+
async cropImage(mediaId, cropOptions) {
|
|
540
|
+
return this.post(`/${mediaId}/crop`, cropOptions);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* 压缩图片
|
|
545
|
+
* @param {string} mediaId - 媒体 ID
|
|
546
|
+
* @param {Object} compressOptions - 压缩参数
|
|
547
|
+
*/
|
|
548
|
+
async compressImage(mediaId, compressOptions) {
|
|
549
|
+
return this.post(`/${mediaId}/compress`, compressOptions);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// ============ 视频处理 ============
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* 获取视频封面
|
|
556
|
+
* @param {string} mediaId - 媒体 ID
|
|
557
|
+
* @param {number} time - 时间点(秒)
|
|
558
|
+
*/
|
|
559
|
+
async getVideoCover(mediaId, time = 0) {
|
|
560
|
+
return this.get(`/${mediaId}/cover`, {
|
|
561
|
+
time
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* 转码视频
|
|
567
|
+
* @param {string} mediaId - 媒体 ID
|
|
568
|
+
* @param {Object} transcodeOptions - 转码参数
|
|
569
|
+
*/
|
|
570
|
+
async transcodeVideo(mediaId, transcodeOptions) {
|
|
571
|
+
return this.post(`/${mediaId}/transcode`, transcodeOptions);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* 获取转码状态
|
|
576
|
+
* @param {string} mediaId - 媒体 ID
|
|
577
|
+
*/
|
|
578
|
+
async getTranscodeStatus(mediaId) {
|
|
579
|
+
return this.get(`/${mediaId}/transcode/status`);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// ============ 存储统计 ============
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* 获取存储使用情况
|
|
586
|
+
*/
|
|
587
|
+
async getStorageUsage() {
|
|
588
|
+
return this.get('/usage');
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* 获取配额信息
|
|
593
|
+
*/
|
|
594
|
+
async getQuota() {
|
|
595
|
+
return this.get('/quota');
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
// 创建默认实例
|
|
600
|
+
const mediaApi = new MediaApiClient();
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Media SDK - React Hooks
|
|
604
|
+
*/
|
|
605
|
+
|
|
606
|
+
function useMedia() {
|
|
607
|
+
const [media, setMedia] = React.useState([]);
|
|
608
|
+
const [uploading, setUploading] = React.useState(false);
|
|
609
|
+
const [progress, setProgress] = React.useState(0);
|
|
610
|
+
const [error, setError] = React.useState(null);
|
|
611
|
+
const fetchMedia = React.useCallback(async (params = {}) => {
|
|
612
|
+
try {
|
|
613
|
+
const result = await mediaApi.getMedia(params);
|
|
614
|
+
setMedia(result.data || []);
|
|
615
|
+
} catch (err) {
|
|
616
|
+
setError(err.message);
|
|
617
|
+
}
|
|
618
|
+
}, []);
|
|
619
|
+
const uploadMedia = React.useCallback(async (file, options = {}) => {
|
|
620
|
+
setUploading(true);
|
|
621
|
+
setProgress(0);
|
|
622
|
+
try {
|
|
623
|
+
const result = await mediaApi.uploadMedia(file, {
|
|
624
|
+
...options,
|
|
625
|
+
onProgress: p => setProgress(p)
|
|
626
|
+
});
|
|
627
|
+
await fetchMedia();
|
|
628
|
+
return result;
|
|
629
|
+
} catch (err) {
|
|
630
|
+
setError(err.message);
|
|
631
|
+
throw err;
|
|
632
|
+
} finally {
|
|
633
|
+
setUploading(false);
|
|
634
|
+
setProgress(0);
|
|
635
|
+
}
|
|
636
|
+
}, [fetchMedia]);
|
|
637
|
+
const deleteMedia = React.useCallback(async mediaId => {
|
|
638
|
+
await mediaApi.deleteMedia(mediaId);
|
|
639
|
+
await fetchMedia();
|
|
640
|
+
}, [fetchMedia]);
|
|
641
|
+
return {
|
|
642
|
+
media,
|
|
643
|
+
uploading,
|
|
644
|
+
progress,
|
|
645
|
+
error,
|
|
646
|
+
fetchMedia,
|
|
647
|
+
uploadMedia,
|
|
648
|
+
deleteMedia
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* Media SDK - 国际化
|
|
654
|
+
*/
|
|
655
|
+
|
|
656
|
+
const messages = {
|
|
657
|
+
zh: {
|
|
658
|
+
'media.title': '媒体库',
|
|
659
|
+
'media.upload': '上传',
|
|
660
|
+
'media.delete': '删除',
|
|
661
|
+
'media.preview': '预览',
|
|
662
|
+
'media.download': '下载',
|
|
663
|
+
'media.types.image': '图片',
|
|
664
|
+
'media.types.video': '视频',
|
|
665
|
+
'media.types.audio': '音频',
|
|
666
|
+
'media.types.document': '文档',
|
|
667
|
+
'media.status.uploading': '上传中...',
|
|
668
|
+
'media.status.uploading_progress': '上传中 {progress}%',
|
|
669
|
+
'media.status.success': '上传成功',
|
|
670
|
+
'media.status.failed': '上传失败',
|
|
671
|
+
'media.empty': '暂无媒体文件',
|
|
672
|
+
'media.usage': '存储使用',
|
|
673
|
+
'media.limit': '存储限制',
|
|
674
|
+
'media.select_file': '选择文件',
|
|
675
|
+
'loading': '加载中...'
|
|
676
|
+
},
|
|
677
|
+
en: {
|
|
678
|
+
'media.title': 'Media Library',
|
|
679
|
+
'media.upload': 'Upload',
|
|
680
|
+
'media.delete': 'Delete',
|
|
681
|
+
'media.preview': 'Preview',
|
|
682
|
+
'media.download': 'Download',
|
|
683
|
+
'media.types.image': 'Image',
|
|
684
|
+
'media.types.video': 'Video',
|
|
685
|
+
'media.types.audio': 'Audio',
|
|
686
|
+
'media.types.document': 'Document',
|
|
687
|
+
'media.status.uploading': 'Uploading...',
|
|
688
|
+
'media.status.uploading_progress': 'Uploading {progress}%',
|
|
689
|
+
'media.status.success': 'Upload successful',
|
|
690
|
+
'media.status.failed': 'Upload failed',
|
|
691
|
+
'media.empty': 'No media files',
|
|
692
|
+
'media.usage': 'Storage Used',
|
|
693
|
+
'media.limit': 'Storage Limit',
|
|
694
|
+
'media.select_file': 'Select File',
|
|
695
|
+
'loading': 'Loading...'
|
|
696
|
+
},
|
|
697
|
+
ja: {
|
|
698
|
+
'media.title': 'メディアライブラリ',
|
|
699
|
+
'media.upload': 'アップロード',
|
|
700
|
+
'media.delete': '削除',
|
|
701
|
+
'media.preview': 'プレビュー',
|
|
702
|
+
'media.download': 'ダウンロード',
|
|
703
|
+
'media.types.image': '画像',
|
|
704
|
+
'media.types.video': '動画',
|
|
705
|
+
'media.types.audio': '音声',
|
|
706
|
+
'media.types.document': 'ドキュメント',
|
|
707
|
+
'media.status.uploading': 'アップロード中...',
|
|
708
|
+
'media.status.uploading_progress': 'アップロード中 {progress}%',
|
|
709
|
+
'media.status.success': 'アップロード成功',
|
|
710
|
+
'media.status.failed': 'アップロード失敗',
|
|
711
|
+
'media.empty': 'メディアファイルがありません',
|
|
712
|
+
'media.usage': 'ストレージ使用量',
|
|
713
|
+
'media.limit': 'ストレージ制限',
|
|
714
|
+
'media.select_file': 'ファイルを選択',
|
|
715
|
+
'loading': '読み込み中...'
|
|
716
|
+
},
|
|
717
|
+
ko: {
|
|
718
|
+
'media.title': '미디어 라이브러리',
|
|
719
|
+
'media.upload': '업로드',
|
|
720
|
+
'media.delete': '삭제',
|
|
721
|
+
'media.preview': '미리보기',
|
|
722
|
+
'media.download': '다운로드',
|
|
723
|
+
'media.types.image': '이미지',
|
|
724
|
+
'media.types.video': '비디오',
|
|
725
|
+
'media.types.audio': '오디오',
|
|
726
|
+
'media.types.document': '문서',
|
|
727
|
+
'media.status.uploading': '업로드 중...',
|
|
728
|
+
'media.status.uploading_progress': '업로드 중 {progress}%',
|
|
729
|
+
'media.status.success': '업로드 성공',
|
|
730
|
+
'media.status.failed': '업로드 실패',
|
|
731
|
+
'media.empty': '미디어 파일이 없습니다',
|
|
732
|
+
'media.usage': '스토리지 사용량',
|
|
733
|
+
'media.limit': '스토리지 제한',
|
|
734
|
+
'media.select_file': '파일 선택',
|
|
735
|
+
'loading': '로딩 중...'
|
|
736
|
+
}
|
|
737
|
+
};
|
|
738
|
+
let currentLanguage = 'en';
|
|
739
|
+
function t(key, params = {}) {
|
|
740
|
+
let text = messages[currentLanguage]?.[key] || messages.en?.[key] || key;
|
|
741
|
+
Object.entries(params).forEach(([k, v]) => {
|
|
742
|
+
text = text.replace(new RegExp(`\\{${k}\\}`, 'g'), v);
|
|
743
|
+
});
|
|
744
|
+
return text;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/**
|
|
748
|
+
* Media SDK - 组件
|
|
749
|
+
*/
|
|
750
|
+
|
|
751
|
+
function MediaGallery({
|
|
752
|
+
onSelect
|
|
753
|
+
}) {
|
|
754
|
+
const {
|
|
755
|
+
media,
|
|
756
|
+
loading,
|
|
757
|
+
deleteMedia
|
|
758
|
+
} = useMedia();
|
|
759
|
+
if (loading) return /*#__PURE__*/React.createElement("div", {
|
|
760
|
+
className: "eco-loading"
|
|
761
|
+
}, t('loading'));
|
|
762
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
763
|
+
className: "eco-media-gallery"
|
|
764
|
+
}, media.map(item => /*#__PURE__*/React.createElement("div", {
|
|
765
|
+
key: item.id,
|
|
766
|
+
className: "eco-media-item",
|
|
767
|
+
onClick: () => onSelect?.(item)
|
|
768
|
+
}, item.type === 'image' ? /*#__PURE__*/React.createElement("img", {
|
|
769
|
+
src: item.url,
|
|
770
|
+
alt: item.name
|
|
771
|
+
}) : /*#__PURE__*/React.createElement("div", {
|
|
772
|
+
className: "eco-media-icon"
|
|
773
|
+
}, item.type === 'video' ? '🎬' : '📄'), /*#__PURE__*/React.createElement("div", {
|
|
774
|
+
className: "eco-media-name"
|
|
775
|
+
}, item.name), /*#__PURE__*/React.createElement("button", {
|
|
776
|
+
className: "eco-btn eco-btn-sm eco-btn-danger",
|
|
777
|
+
onClick: e => {
|
|
778
|
+
e.stopPropagation();
|
|
779
|
+
deleteMedia(item.id);
|
|
780
|
+
}
|
|
781
|
+
}, t('media.delete')))));
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
exports.AudioPlayer = AudioPlayer;
|
|
785
|
+
exports.Gallery = Gallery;
|
|
786
|
+
exports.ImageViewer = ImageViewer;
|
|
787
|
+
exports.MediaGallery = MediaGallery;
|
|
788
|
+
exports.VideoPlayer = VideoPlayer;
|
|
789
|
+
exports.useMedia = useMedia;
|
|
790
|
+
//# sourceMappingURL=index.cjs.map
|