Upload-Freigabe: URL-Shortener entfernt, einfaches Kopieren der Original-URL
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const https = require('https');
|
|
||||||
const bcrypt = require('bcryptjs');
|
const bcrypt = require('bcryptjs');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const multer = require('multer');
|
const multer = require('multer');
|
||||||
@@ -9,19 +8,6 @@ const db = require('../../db');
|
|||||||
const { authenticate, requireAdmin } = require('../../middleware/auth');
|
const { authenticate, requireAdmin } = require('../../middleware/auth');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
// Node-seitige URL-Kürzung (kein CORS-Problem)
|
|
||||||
function shortenUrl(longUrl) {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
const url = `https://is.gd/create.php?format=simple&url=${encodeURIComponent(longUrl)}`;
|
|
||||||
const req = https.get(url, { headers:{'User-Agent':'DickenDock/1.0'} }, res => {
|
|
||||||
let data = '';
|
|
||||||
res.on('data', d => data += d);
|
|
||||||
res.on('end', () => resolve(data.trim().startsWith('http') ? data.trim() : null));
|
|
||||||
});
|
|
||||||
req.on('error', () => resolve(null));
|
|
||||||
req.setTimeout(5000, () => { req.destroy(); resolve(null); });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const UPLOAD_DIR = process.env.UPLOAD_DIR || '/data/uploads';
|
const UPLOAD_DIR = process.env.UPLOAD_DIR || '/data/uploads';
|
||||||
|
|
||||||
@@ -55,7 +41,7 @@ router.get('/', authenticate, (req, res) => {
|
|||||||
res.json({ shares, folder });
|
res.json({ shares, folder });
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/', authenticate, async (req, res) => {
|
router.post('/', authenticate, (req, res) => {
|
||||||
if (!canUseUploadShare(req.user.id)) return res.status(403).json({ error:'Keine Berechtigung' });
|
if (!canUseUploadShare(req.user.id)) return res.status(403).json({ error:'Keine Berechtigung' });
|
||||||
const { password, expires_hours=24, max_size_mb=10 } = req.body;
|
const { password, expires_hours=24, max_size_mb=10 } = req.body;
|
||||||
if (!password?.trim()) return res.status(400).json({ error:'Passwort erforderlich' });
|
if (!password?.trim()) return res.status(400).json({ error:'Passwort erforderlich' });
|
||||||
@@ -67,14 +53,7 @@ router.post('/', authenticate, async (req, res) => {
|
|||||||
INSERT INTO upload_shares (user_id,token,password_hash,expires_at,max_size_mb,folder_id,created_at)
|
INSERT INTO upload_shares (user_id,token,password_hash,expires_at,max_size_mb,folder_id,created_at)
|
||||||
VALUES (?,?,?,?,?,?,datetime('now','localtime'))
|
VALUES (?,?,?,?,?,?,datetime('now','localtime'))
|
||||||
`).run(req.user.id, token, hash, expires, Number(max_size_mb), folder.id);
|
`).run(req.user.id, token, hash, expires, Number(max_size_mb), folder.id);
|
||||||
const share = db.prepare('SELECT * FROM upload_shares WHERE id=?').get(r.lastInsertRowid);
|
res.json(db.prepare('SELECT * FROM upload_shares WHERE id=?').get(r.lastInsertRowid));
|
||||||
// URL direkt beim Erstellen kürzen (server-seitig, kein CORS)
|
|
||||||
const baseUrl = `${req.protocol}://${req.get('host')}`;
|
|
||||||
const fullUrl = `${baseUrl}/u/${token}`;
|
|
||||||
let shortUrl = null;
|
|
||||||
try { shortUrl = await shortenUrl(fullUrl); } catch {}
|
|
||||||
if (shortUrl) db.prepare('UPDATE upload_shares SET short_url=? WHERE id=?').run(shortUrl, share.id);
|
|
||||||
res.json({ ...share, short_url: shortUrl, url: fullUrl });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.delete('/:id', authenticate, (req, res) => {
|
router.delete('/:id', authenticate, (req, res) => {
|
||||||
|
|||||||
@@ -218,24 +218,20 @@ function UploadShareManager({ toast }) {
|
|||||||
setShortening(null);
|
setShortening(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
const copyLink = (shareId, token, shortUrl) => {
|
const copyLink = (token) => {
|
||||||
// Kurz-URL vom Server nehmen (bereits beim Erstellen generiert), sonst Original
|
const url = `${window.location.origin}/u/${token}`;
|
||||||
const url = shortUrl || `${window.location.origin}/u/${token}`;
|
|
||||||
// Synchron kopieren – kein await nötig
|
|
||||||
if (navigator.clipboard) {
|
if (navigator.clipboard) {
|
||||||
navigator.clipboard.writeText(url).catch(() => fallbackCopy(url));
|
navigator.clipboard.writeText(url).catch(() => {
|
||||||
} else { fallbackCopy(url); }
|
const ta = document.createElement('textarea');
|
||||||
|
ta.value = url; ta.style.cssText='position:fixed;opacity:0.01';
|
||||||
|
document.body.appendChild(ta); ta.focus(); ta.select();
|
||||||
|
try { document.execCommand('copy'); } catch {}
|
||||||
|
document.body.removeChild(ta);
|
||||||
|
});
|
||||||
|
}
|
||||||
setCopied(token); setTimeout(()=>setCopied(null), 2500);
|
setCopied(token); setTimeout(()=>setCopied(null), 2500);
|
||||||
};
|
};
|
||||||
|
|
||||||
const fallbackCopy = (text) => {
|
|
||||||
const ta = document.createElement('textarea');
|
|
||||||
ta.value = text; ta.style.cssText = 'position:fixed;top:0;left:0;opacity:0.01';
|
|
||||||
document.body.appendChild(ta); ta.focus(); ta.select();
|
|
||||||
try { document.execCommand('copy'); } catch {}
|
|
||||||
document.body.removeChild(ta);
|
|
||||||
};
|
|
||||||
|
|
||||||
const loadLogs = () => {
|
const loadLogs = () => {
|
||||||
api('/upload-shares/logs').then(setLogs).catch(()=>{});
|
api('/upload-shares/logs').then(setLogs).catch(()=>{});
|
||||||
setShowLogs(true);
|
setShowLogs(true);
|
||||||
@@ -345,7 +341,7 @@ function UploadShareManager({ toast }) {
|
|||||||
<div style={{display:'flex',gap:5,flexShrink:0}}>
|
<div style={{display:'flex',gap:5,flexShrink:0}}>
|
||||||
{!!s.is_active && !expired && (
|
{!!s.is_active && !expired && (
|
||||||
<>
|
<>
|
||||||
<button onClick={()=>copyLink(s.id, s.token, s.short_url)}
|
<button onClick={()=>copyLink(s.token)}
|
||||||
style={{...S.btn(copied===s.token?'#4ecdc4':'#ffe66d',copied!==s.token),fontSize:10,padding:'4px 10px'}}>
|
style={{...S.btn(copied===s.token?'#4ecdc4':'#ffe66d',copied!==s.token),fontSize:10,padding:'4px 10px'}}>
|
||||||
{copied===s.token?'✓ Kopiert':'📋 Link'}
|
{copied===s.token?'✓ Kopiert':'📋 Link'}
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
Reference in New Issue
Block a user