update nextcloud for file support
This commit is contained in:
@@ -167,6 +167,117 @@ class NextcloudController {
|
||||
}
|
||||
}
|
||||
|
||||
async uploadFile(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const credentials = await userService.getNextcloudCredentials(req.user!.id);
|
||||
if (!credentials) {
|
||||
res.status(401).json({ success: false, message: 'Nextcloud nicht verbunden' });
|
||||
return;
|
||||
}
|
||||
const token = req.params.token as string;
|
||||
if (!token) {
|
||||
res.status(400).json({ success: false, message: 'Room token fehlt' });
|
||||
return;
|
||||
}
|
||||
if (!req.file) {
|
||||
res.status(400).json({ success: false, message: 'Keine Datei übermittelt' });
|
||||
return;
|
||||
}
|
||||
await nextcloudService.uploadFileToTalk(
|
||||
token,
|
||||
req.file.buffer,
|
||||
req.file.originalname,
|
||||
req.file.mimetype,
|
||||
credentials.loginName,
|
||||
credentials.appPassword,
|
||||
);
|
||||
res.status(200).json({ success: true, data: null });
|
||||
} catch (error: any) {
|
||||
if (error?.code === 'NEXTCLOUD_AUTH_INVALID') {
|
||||
await userService.clearNextcloudCredentials(req.user!.id);
|
||||
res.status(200).json({ success: true, data: { connected: false } });
|
||||
return;
|
||||
}
|
||||
logger.error('uploadFile error', { error });
|
||||
res.status(500).json({ success: false, message: 'Datei konnte nicht hochgeladen werden' });
|
||||
}
|
||||
}
|
||||
|
||||
async downloadFile(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const credentials = await userService.getNextcloudCredentials(req.user!.id);
|
||||
if (!credentials) {
|
||||
res.status(401).json({ success: false, message: 'Nextcloud nicht verbunden' });
|
||||
return;
|
||||
}
|
||||
const filePath = req.query.path as string;
|
||||
if (!filePath) {
|
||||
res.status(400).json({ success: false, message: 'Dateipfad fehlt' });
|
||||
return;
|
||||
}
|
||||
const response = await nextcloudService.downloadFile(
|
||||
filePath,
|
||||
credentials.loginName,
|
||||
credentials.appPassword,
|
||||
);
|
||||
const contentType = response.headers['content-type'] ?? 'application/octet-stream';
|
||||
const contentDisposition = response.headers['content-disposition'] ?? `attachment; filename="${req.params.fileId}"`;
|
||||
res.setHeader('Content-Type', contentType);
|
||||
res.setHeader('Content-Disposition', contentDisposition);
|
||||
if (response.headers['content-length']) {
|
||||
res.setHeader('Content-Length', response.headers['content-length']);
|
||||
}
|
||||
response.data.pipe(res);
|
||||
} catch (error: any) {
|
||||
if (error?.code === 'NEXTCLOUD_AUTH_INVALID') {
|
||||
await userService.clearNextcloudCredentials(req.user!.id);
|
||||
res.status(401).json({ success: false, message: 'Nextcloud nicht verbunden' });
|
||||
return;
|
||||
}
|
||||
logger.error('downloadFile error', { error });
|
||||
res.status(500).json({ success: false, message: 'Datei konnte nicht heruntergeladen werden' });
|
||||
}
|
||||
}
|
||||
|
||||
async getFilePreview(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const credentials = await userService.getNextcloudCredentials(req.user!.id);
|
||||
if (!credentials) {
|
||||
res.status(401).json({ success: false, message: 'Nextcloud nicht verbunden' });
|
||||
return;
|
||||
}
|
||||
const fileId = parseInt(req.params.fileId as string, 10);
|
||||
if (isNaN(fileId)) {
|
||||
res.status(400).json({ success: false, message: 'Ungültige Datei-ID' });
|
||||
return;
|
||||
}
|
||||
const w = parseInt((req.query.w as string) ?? '400', 10) || 400;
|
||||
const h = parseInt((req.query.h as string) ?? '400', 10) || 400;
|
||||
const response = await nextcloudService.getFilePreview(
|
||||
fileId,
|
||||
Math.min(w, 1200),
|
||||
Math.min(h, 1200),
|
||||
credentials.loginName,
|
||||
credentials.appPassword,
|
||||
);
|
||||
const contentType = response.headers['content-type'] ?? 'image/jpeg';
|
||||
res.setHeader('Content-Type', contentType);
|
||||
if (response.headers['content-length']) {
|
||||
res.setHeader('Content-Length', response.headers['content-length']);
|
||||
}
|
||||
res.setHeader('Cache-Control', 'private, max-age=300');
|
||||
response.data.pipe(res);
|
||||
} catch (error: any) {
|
||||
if (error?.code === 'NEXTCLOUD_AUTH_INVALID') {
|
||||
await userService.clearNextcloudCredentials(req.user!.id);
|
||||
res.status(401).json({ success: false, message: 'Nextcloud nicht verbunden' });
|
||||
return;
|
||||
}
|
||||
logger.error('getFilePreview error', { error });
|
||||
res.status(500).json({ success: false, message: 'Vorschau konnte nicht geladen werden' });
|
||||
}
|
||||
}
|
||||
|
||||
async markRoomAsRead(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const credentials = await userService.getNextcloudCredentials(req.user!.id);
|
||||
|
||||
Reference in New Issue
Block a user