polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
288 lines • 12.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocumentHandler = void 0;
const base_handler_1 = require("./base.handler");
const document_service_sdk_1 = require("../services/document.service.sdk");
const confirmation_1 = require("../utils/confirmation");
const api_command_1 = require("../utils/api-command");
class DocumentHandler extends base_handler_1.BaseHandler {
constructor(authConfig, serviceConfig, documentService) {
super();
this.documentService = documentService ?? new document_service_sdk_1.DocumentServiceSdk(authConfig, serviceConfig);
}
async listDocuments(options) {
return this.executeWithErrorHandling(async () => {
if (!options.channelId || options.channelId.trim() === '') {
throw new Error('channelId is required');
}
const result = await this.documentService.getDocumentList(options);
this.displayDocumentList(result.contents, options.channelId, options.output);
}, 'document.list');
}
async uploadDocument(options) {
return this.executeWithErrorHandling(async () => {
if (!options.channelId || options.channelId.trim() === '') {
throw new Error('channelId is required');
}
if (!options.url || options.url.trim() === '') {
throw new Error('url is required');
}
const uploadOptions = { url: options.url };
if (options.type !== undefined) {
uploadOptions.type = options.type;
}
if (options.docName !== undefined) {
uploadOptions.docName = options.docName;
}
if (options.callbackUrl !== undefined) {
uploadOptions.callbackUrl = options.callbackUrl;
}
const result = await this.documentService.uploadDocument(options.channelId, uploadOptions);
this.displayUploadResult(options.channelId, result, options.output, options.docName);
}, 'document.upload');
}
async deleteDocument(options) {
return this.executeWithErrorHandling(async () => {
if (!options.channelId || options.channelId.trim() === '') {
throw new Error('channelId is required');
}
if (!options.fileId || options.fileId.trim() === '') {
throw new Error('fileId is required');
}
if (!options.force) {
if (!(0, confirmation_1.isInteractiveEnvironment)()) {
throw new Error('Interactive confirmation not available in non-TTY environment. Use --force flag to bypass confirmation.');
}
}
const listResult = await this.documentService.getDocumentList({
channelId: options.channelId,
});
const doc = listResult.contents.find((item) => item.fileId === options.fileId);
if (!doc) {
throw new Error(`Document not found: ${options.fileId}`);
}
const docType = (options.type || doc.type);
if (!options.force) {
const confirmed = await (0, confirmation_1.confirmDeletion)(`确定要删除文档 '${doc.fileName}' (${options.fileId}) 吗?此操作无法撤销。`, 'yes');
if (!confirmed) {
this.displayInfo('删除操作已取消');
return;
}
}
await this.documentService.deleteDocument(options.channelId, options.fileId, docType);
this.displayDeleteResult(options, doc);
}, 'document.delete');
}
async getDocumentStatus(options) {
return this.executeWithErrorHandling(async () => {
if (!options.channelId || options.channelId.trim() === '') {
throw new Error('channelId is required');
}
if (!options.fileId || options.fileId.trim() === '') {
throw new Error('fileId is required');
}
const result = await this.documentService.getDocumentStatus(options.channelId, options.fileId);
this.displayStatusResult(options.channelId, result, options.output);
}, 'document.status');
}
async updateTeacherDocRelation(options) {
return this.executeWithErrorHandling(async () => {
await (0, api_command_1.confirmWrite)(options.force, `Update teacher ${options.teacherId} document relation?`);
const result = await this.documentService.updateTeacherDocRelation(options.teacherId, this.normalizeStringList(options.fileIds).join(','), options.operation);
this.displayResult({ teacherId: options.teacherId, operation: options.operation, success: result }, options.output);
}, 'document.teacher-doc.relation');
}
async listChannelMultimediaResourceVids(options) {
return this.executeWithErrorHandling(async () => {
const result = await this.documentService.getChannelMultimediaResourceList(options.channelId, this.paginationOptions(options));
this.displayResult(result, options.output);
}, 'document.media.vids');
}
async listChannelMultimediaResourceDetails(options) {
return this.executeWithErrorHandling(async () => {
const result = await this.documentService.getChannelMultimediaResourceDetail(options.channelId, this.paginationOptions(options));
this.displayResult(result, options.output);
}, 'document.media.details');
}
async linkChannelMultimediaResource(options) {
return this.executeWithErrorHandling(async () => {
const vids = this.normalizeStringList(options.vids).join(',');
await (0, api_command_1.confirmWrite)(options.force, `Link ${vids} to channel ${options.channelId}?`);
const result = await this.documentService.linkChannelMultimediaResource(options.channelId, vids);
this.displayResult({ channelId: options.channelId, vids, success: result }, options.output);
}, 'document.media.link');
}
async unlinkChannelMultimediaResource(options) {
return this.executeWithErrorHandling(async () => {
const vids = this.normalizeStringList(options.vids).join(',');
await (0, api_command_1.confirmWrite)(options.force, `Unlink ${vids} from channel ${options.channelId}?`);
const result = await this.documentService.unlinkChannelMultimediaResource(options.channelId, vids);
this.displayResult({ channelId: options.channelId, vids, success: result }, options.output);
}, 'document.media.unlink');
}
async getUserMultimediaResourceDetail(options) {
return this.executeWithErrorHandling(async () => {
const result = await this.documentService.getUserMultimediaResourceDetail(this.normalizeStringList(options.vids).join(','));
this.displayResult(result, options.output);
}, 'document.media.user-detail');
}
async deleteUserMultimediaResource(options) {
return this.executeWithErrorHandling(async () => {
const vids = this.normalizeStringList(options.vids).join(',');
await (0, api_command_1.confirmWrite)(options.force, `Delete user multimedia resource(s) ${vids}?`);
const result = await this.documentService.deleteUserMultimediaResource(vids);
this.displayResult({ vids, success: result }, options.output);
}, 'document.media.user-delete');
}
displayDocumentList(contents, channelId, format = 'table') {
if (contents.length === 0) {
this.displayInfo(`暂无课件文档 - 频道: ${channelId}`);
return;
}
this.displayInfo(`课件列表 - 频道: ${channelId}`);
this.displayInfo(`共 ${contents.length} 个文档`);
if (format === 'json') {
this.displayData(contents, 'json');
}
else {
this.displayDocumentTable(contents);
}
}
normalizeStringList(value) {
const items = Array.isArray(value) ? value : String(value ?? '').split(',');
const list = items.map((item) => String(item).trim()).filter(Boolean);
if (list.length === 0) {
throw new Error('list must not be empty');
}
return list;
}
paginationOptions(options) {
return {
...(options.page !== undefined ? { pageNumber: options.page } : {}),
...(options.pageSize !== undefined ? { pageSize: options.pageSize } : {}),
};
}
displayResult(result, format = 'table') {
this.displayData(result ?? { success: true }, format);
}
displayDocumentTable(contents) {
const tableData = contents.map((item) => ({
'文件ID': item.fileId,
'文件名': item.fileName,
'类型': item.fileType,
'状态': this.translateStatus(item.status),
'页数': item.totalPage || '-',
'创建时间': this.formatTimestamp(item.createTime),
}));
this.displayAsTable(tableData);
}
displayUploadResult(channelId, result, format = 'table', docName) {
const resultData = {
channelId,
fileName: docName || '未知',
fileId: result.fileId,
convertType: result.type,
status: this.translateStatus(result.status),
};
this.displayInfo(`上传成功`);
this.displayInfo(`课件文档上传 - 频道: ${channelId}`);
if (format === 'json') {
this.displayData(resultData, 'json');
}
else {
this.displayUploadResultTable(resultData);
}
}
displayUploadResultTable(resultData) {
const tableData = [{
'频道': resultData.channelId,
'文件名': resultData.fileName,
'文件ID': resultData.fileId,
'转换类型': resultData.convertType,
'状态': resultData.status,
}];
this.displayAsTable(tableData);
}
displayDeleteResult(options, docInfo) {
const format = options.output || 'table';
const resultData = {
channelId: options.channelId,
fileId: options.fileId,
fileName: docInfo.fileName,
status: '已删除',
};
this.displayInfo(`删除成功`);
this.displayInfo(`课件文档删除 - 频道: ${options.channelId}`);
if (format === 'json') {
this.displayData(resultData, 'json');
}
else {
this.displayDeleteResultTable(resultData);
}
}
displayDeleteResultTable(resultData) {
const tableData = [{
'频道': resultData.channelId,
'文件ID': resultData.fileId,
'文件名': resultData.fileName,
'状态': resultData.status,
}];
this.displayAsTable(tableData);
}
displayStatusResult(channelId, result, format = 'table') {
this.displayInfo(`转码状态 - 频道: ${channelId}`);
if (result.length === 0) {
this.displayInfo('未找到文档转码状态');
return;
}
if (format === 'json') {
this.displayData(result, 'json');
}
else {
this.displayStatusTable(result);
}
}
displayStatusTable(result) {
const tableData = result.map((item) => ({
'文件ID': item.fileId,
'状态': this.translateConvertStatus(item.convertStatus),
'类型': item.type,
'页数': item.totalPage || '-',
'图片数量': item.imageCount || '-',
}));
this.displayAsTable(tableData);
}
translateStatus(status) {
const statusMap = {
normal: '正常',
waitUpload: '等待上传',
failUpload: '上传失败',
waitConvert: '转换中',
failConvert: '转换失败',
};
return statusMap[status] || status;
}
translateConvertStatus(status) {
const statusMap = {
normal: '正常',
waiting: '等待中',
processing: '处理中',
fail: '失败',
};
return statusMap[status] || status;
}
formatTimestamp(timestamp) {
if (!timestamp)
return '-';
const date = new Date(timestamp);
return date.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
});
}
}
exports.DocumentHandler = DocumentHandler;
//# sourceMappingURL=document.handler.js.map