2025-03-28 08:09:18 +01:00
|
|
|
/**
|
2026-02-15 23:45:11 +01:00
|
|
|
* OxiCloud - Shared Resources Page (/shared)
|
|
|
|
|
* All operations go through the backend API at /api/shares.
|
2025-03-28 08:09:18 +01:00
|
|
|
*/
|
|
|
|
|
|
2026-02-15 23:45:11 +01:00
|
|
|
// Authentication check
|
2025-03-28 08:09:18 +01:00
|
|
|
function checkAuthentication() {
|
2026-02-15 23:45:11 +01:00
|
|
|
const token = localStorage.getItem('oxicloud_token');
|
|
|
|
|
const tokenExpiry = localStorage.getItem('oxicloud_token_expiry');
|
2025-03-28 08:09:18 +01:00
|
|
|
if (!token || !tokenExpiry || new Date(tokenExpiry) < new Date()) {
|
2026-02-13 22:44:20 +01:00
|
|
|
window.location.href = '/login';
|
2025-03-28 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-28 09:03:04 +01:00
|
|
|
document.addEventListener('DOMContentLoaded', async () => {
|
2026-02-15 23:45:11 +01:00
|
|
|
// ── i18n ──
|
|
|
|
|
if (window.i18n && window.i18n.init) {
|
|
|
|
|
await window.i18n.init();
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if (window.i18n.translatePage) window.i18n.translatePage();
|
|
|
|
|
}, 500);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function t(key, fallback) {
|
|
|
|
|
return (window.i18n && window.i18n.t) ? window.i18n.t(key, fallback) : fallback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Auth headers helper ──
|
|
|
|
|
function authHeaders(json = false) {
|
|
|
|
|
const h = {};
|
|
|
|
|
const token = localStorage.getItem('oxicloud_token');
|
|
|
|
|
if (token) h['Authorization'] = `Bearer ${token}`;
|
|
|
|
|
if (json) h['Content-Type'] = 'application/json';
|
|
|
|
|
return h;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Elements ──
|
2025-03-28 08:09:18 +01:00
|
|
|
const sharedItemsList = document.getElementById('shared-items-list');
|
|
|
|
|
const emptySharedState = document.getElementById('empty-shared-state');
|
|
|
|
|
const filterType = document.getElementById('filter-type');
|
|
|
|
|
const sortBy = document.getElementById('sort-by');
|
|
|
|
|
const sharedSearch = document.getElementById('shared-search');
|
|
|
|
|
const sharedSearchBtn = document.getElementById('shared-search-btn');
|
|
|
|
|
const goToFilesBtn = document.getElementById('go-to-files');
|
2026-02-15 23:45:11 +01:00
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
// Share dialog elements
|
|
|
|
|
const shareDialog = document.getElementById('share-dialog');
|
2026-02-15 23:45:11 +01:00
|
|
|
const shareDialogCloseBtn = shareDialog ? shareDialog.querySelector('.close-dialog-btn') : null;
|
2025-03-28 08:09:18 +01:00
|
|
|
const shareDialogIcon = document.getElementById('share-dialog-icon');
|
|
|
|
|
const shareDialogName = document.getElementById('share-dialog-name');
|
|
|
|
|
const shareLinkUrl = document.getElementById('share-link-url');
|
|
|
|
|
const copyLinkBtn = document.getElementById('copy-link-btn');
|
|
|
|
|
const enablePassword = document.getElementById('enable-password');
|
|
|
|
|
const sharePassword = document.getElementById('share-password');
|
|
|
|
|
const generatePasswordBtn = document.getElementById('generate-password');
|
|
|
|
|
const enableExpiration = document.getElementById('enable-expiration');
|
|
|
|
|
const shareExpiration = document.getElementById('share-expiration');
|
|
|
|
|
const permissionRead = document.getElementById('permission-read');
|
|
|
|
|
const permissionWrite = document.getElementById('permission-write');
|
|
|
|
|
const permissionReshare = document.getElementById('permission-reshare');
|
|
|
|
|
const updateShareBtn = document.getElementById('update-share-btn');
|
|
|
|
|
const removeShareBtn = document.getElementById('remove-share-btn');
|
2026-02-15 23:45:11 +01:00
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
// Notification dialog elements
|
|
|
|
|
const notificationDialog = document.getElementById('share-notification-dialog');
|
2026-02-15 23:45:11 +01:00
|
|
|
const notificationCloseBtn = notificationDialog ? notificationDialog.querySelector('.close-dialog-btn') : null;
|
2025-03-28 08:09:18 +01:00
|
|
|
const notifyDialogIcon = document.getElementById('notify-dialog-icon');
|
|
|
|
|
const notifyDialogName = document.getElementById('notify-dialog-name');
|
|
|
|
|
const notificationEmail = document.getElementById('notification-email');
|
|
|
|
|
const notificationMessage = document.getElementById('notification-message');
|
|
|
|
|
const sendNotificationBtn = document.getElementById('send-notification-btn');
|
2026-02-15 23:45:11 +01:00
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
// Notification banner
|
|
|
|
|
const notificationBanner = document.getElementById('notification-banner');
|
|
|
|
|
const notificationBannerMessage = document.getElementById('notification-message');
|
|
|
|
|
const closeNotificationBtn = document.getElementById('close-notification');
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
// ── State ──
|
2025-03-28 08:09:18 +01:00
|
|
|
let currentSharedItem = null;
|
|
|
|
|
let allSharedItems = [];
|
|
|
|
|
let filteredItems = [];
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
// ── Init ──
|
2025-03-28 08:09:18 +01:00
|
|
|
checkAuthentication();
|
2026-02-15 23:45:11 +01:00
|
|
|
await loadSharedItems();
|
|
|
|
|
|
|
|
|
|
// ── Event listeners ──
|
|
|
|
|
if (filterType) filterType.addEventListener('change', filterAndSortItems);
|
|
|
|
|
if (sortBy) sortBy.addEventListener('change', filterAndSortItems);
|
|
|
|
|
if (sharedSearchBtn) sharedSearchBtn.addEventListener('click', filterAndSortItems);
|
|
|
|
|
if (sharedSearch) sharedSearch.addEventListener('keyup', e => { if (e.key === 'Enter') filterAndSortItems(); });
|
|
|
|
|
if (goToFilesBtn) goToFilesBtn.addEventListener('click', () => window.location.href = '/');
|
|
|
|
|
|
|
|
|
|
if (shareDialogCloseBtn) shareDialogCloseBtn.addEventListener('click', closeShareDialog);
|
|
|
|
|
if (copyLinkBtn) copyLinkBtn.addEventListener('click', copyShareLink);
|
|
|
|
|
if (enablePassword) enablePassword.addEventListener('change', () => {
|
|
|
|
|
if (sharePassword) { sharePassword.disabled = !enablePassword.checked; if (enablePassword.checked) sharePassword.focus(); }
|
2025-03-28 08:09:18 +01:00
|
|
|
});
|
2026-02-15 23:45:11 +01:00
|
|
|
if (generatePasswordBtn) generatePasswordBtn.addEventListener('click', generatePassword);
|
|
|
|
|
if (enableExpiration) enableExpiration.addEventListener('change', () => {
|
|
|
|
|
if (shareExpiration) { shareExpiration.disabled = !enableExpiration.checked; if (enableExpiration.checked) shareExpiration.focus(); }
|
2025-03-28 08:09:18 +01:00
|
|
|
});
|
2026-02-15 23:45:11 +01:00
|
|
|
if (updateShareBtn) updateShareBtn.addEventListener('click', updateSharedItem);
|
|
|
|
|
if (removeShareBtn) removeShareBtn.addEventListener('click', removeSharedItem);
|
|
|
|
|
|
|
|
|
|
if (notificationCloseBtn) notificationCloseBtn.addEventListener('click', closeNotificationDialog);
|
|
|
|
|
if (sendNotificationBtn) sendNotificationBtn.addEventListener('click', sendNotification);
|
|
|
|
|
if (closeNotificationBtn) closeNotificationBtn.addEventListener('click', () => {
|
|
|
|
|
if (notificationBanner) notificationBanner.classList.remove('active');
|
2025-03-28 08:09:18 +01:00
|
|
|
});
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
// ── Load shares from backend ──
|
|
|
|
|
async function loadSharedItems() {
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch('/api/shares?page=1&per_page=1000', {
|
|
|
|
|
headers: authHeaders()
|
|
|
|
|
});
|
|
|
|
|
if (res.ok) {
|
|
|
|
|
const data = await res.json();
|
|
|
|
|
allSharedItems = data.items || [];
|
|
|
|
|
} else {
|
|
|
|
|
allSharedItems = [];
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Error loading shared items:', err);
|
|
|
|
|
allSharedItems = [];
|
|
|
|
|
}
|
2025-03-28 08:09:18 +01:00
|
|
|
filterAndSortItems();
|
|
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
// ── Filter & sort ──
|
2025-03-28 08:09:18 +01:00
|
|
|
function filterAndSortItems() {
|
2026-02-15 23:45:11 +01:00
|
|
|
const type = filterType ? filterType.value : 'all';
|
|
|
|
|
const sort = sortBy ? sortBy.value : 'date';
|
|
|
|
|
const searchTerm = sharedSearch ? sharedSearch.value.toLowerCase() : '';
|
|
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
filteredItems = allSharedItems.filter(item => {
|
2026-02-15 23:45:11 +01:00
|
|
|
if (type !== 'all' && item.item_type !== type) return false;
|
|
|
|
|
const name = (item.item_name || item.item_id || '').toLowerCase();
|
|
|
|
|
return name.includes(searchTerm);
|
2025-03-28 08:09:18 +01:00
|
|
|
});
|
2026-02-15 23:45:11 +01:00
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
filteredItems.sort((a, b) => {
|
|
|
|
|
if (sort === 'name') {
|
2026-02-15 23:45:11 +01:00
|
|
|
return (a.item_name || a.item_id || '').localeCompare(b.item_name || b.item_id || '');
|
2025-03-28 08:09:18 +01:00
|
|
|
} else if (sort === 'date') {
|
2026-02-15 23:45:11 +01:00
|
|
|
return (b.created_at || 0) - (a.created_at || 0);
|
2025-03-28 08:09:18 +01:00
|
|
|
} else if (sort === 'expiration') {
|
2026-02-15 23:45:11 +01:00
|
|
|
if (!a.expires_at && !b.expires_at) return 0;
|
|
|
|
|
if (!a.expires_at) return 1;
|
|
|
|
|
if (!b.expires_at) return -1;
|
|
|
|
|
return a.expires_at - b.expires_at;
|
2025-03-28 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
2026-02-15 23:45:11 +01:00
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
displaySharedItems();
|
|
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
// ── Display table ──
|
2025-03-28 08:09:18 +01:00
|
|
|
function displaySharedItems() {
|
2026-02-15 23:45:11 +01:00
|
|
|
if (!sharedItemsList) return;
|
2025-03-28 08:09:18 +01:00
|
|
|
sharedItemsList.innerHTML = '';
|
2026-02-15 23:45:11 +01:00
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
if (filteredItems.length === 0) {
|
2026-02-15 23:45:11 +01:00
|
|
|
if (emptySharedState) emptySharedState.style.display = 'flex';
|
|
|
|
|
const listContainer = document.querySelector('.shared-list-container');
|
|
|
|
|
if (listContainer) listContainer.style.display = 'none';
|
2025-03-28 08:09:18 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
if (emptySharedState) emptySharedState.style.display = 'none';
|
|
|
|
|
const listContainer = document.querySelector('.shared-list-container');
|
|
|
|
|
if (listContainer) listContainer.style.display = 'block';
|
|
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
filteredItems.forEach(item => {
|
|
|
|
|
const row = document.createElement('tr');
|
2026-02-15 23:45:11 +01:00
|
|
|
const displayName = item.item_name || item.item_id || 'Unknown';
|
|
|
|
|
|
|
|
|
|
// Name
|
2025-03-28 08:09:18 +01:00
|
|
|
const nameCell = document.createElement('td');
|
|
|
|
|
nameCell.className = 'shared-item-name';
|
2026-02-15 23:45:11 +01:00
|
|
|
nameCell.innerHTML = `<span class="item-icon">${item.item_type === 'file' ? '📄' : '📁'}</span><span>${displayName}</span>`;
|
|
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
// Type
|
|
|
|
|
const typeCell = document.createElement('td');
|
2026-02-15 23:45:11 +01:00
|
|
|
typeCell.textContent = item.item_type === 'file' ? t('shared_typeFile', 'File') : t('shared_typeFolder', 'Folder');
|
|
|
|
|
|
|
|
|
|
// Date
|
2025-03-28 08:09:18 +01:00
|
|
|
const dateCell = document.createElement('td');
|
2026-02-15 23:45:11 +01:00
|
|
|
dateCell.textContent = formatDate(item.created_at);
|
|
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
// Expiration
|
|
|
|
|
const expirationCell = document.createElement('td');
|
2026-02-15 23:45:11 +01:00
|
|
|
expirationCell.textContent = item.expires_at ? formatDate(item.expires_at) : t('shared_noExpiration', 'No expiration');
|
|
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
// Permissions
|
|
|
|
|
const permissionsCell = document.createElement('td');
|
2026-02-15 23:45:11 +01:00
|
|
|
const perms = [];
|
|
|
|
|
if (item.permissions?.read) perms.push(t('share_permissionRead', 'Read'));
|
|
|
|
|
if (item.permissions?.write) perms.push(t('share_permissionWrite', 'Write'));
|
|
|
|
|
if (item.permissions?.reshare) perms.push(t('share_permissionReshare', 'Reshare'));
|
|
|
|
|
permissionsCell.textContent = perms.join(', ') || 'Read';
|
|
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
// Password
|
|
|
|
|
const passwordCell = document.createElement('td');
|
2026-02-15 23:45:11 +01:00
|
|
|
passwordCell.textContent = item.has_password ? t('shared_hasPassword', 'Yes') : t('shared_noPassword', 'No');
|
|
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
// Actions
|
|
|
|
|
const actionsCell = document.createElement('td');
|
|
|
|
|
actionsCell.className = 'shared-item-actions';
|
2026-02-15 23:45:11 +01:00
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
const editBtn = document.createElement('button');
|
|
|
|
|
editBtn.className = 'action-btn edit-btn';
|
|
|
|
|
editBtn.innerHTML = '<span class="action-icon">✏️</span>';
|
2026-02-15 23:45:11 +01:00
|
|
|
editBtn.title = t('shared_editShare', 'Edit Share');
|
2025-03-28 08:09:18 +01:00
|
|
|
editBtn.addEventListener('click', () => openShareDialog(item));
|
2026-02-15 23:45:11 +01:00
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
const notifyBtn = document.createElement('button');
|
|
|
|
|
notifyBtn.className = 'action-btn notify-btn';
|
|
|
|
|
notifyBtn.innerHTML = '<span class="action-icon">📧</span>';
|
2026-02-15 23:45:11 +01:00
|
|
|
notifyBtn.title = t('shared_notifyShare', 'Notify Someone');
|
2025-03-28 08:09:18 +01:00
|
|
|
notifyBtn.addEventListener('click', () => openNotificationDialog(item));
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
const cpBtn = document.createElement('button');
|
|
|
|
|
cpBtn.className = 'action-btn copy-btn';
|
|
|
|
|
cpBtn.innerHTML = '<span class="action-icon">📋</span>';
|
|
|
|
|
cpBtn.title = t('shared_copyLink', 'Copy Link');
|
|
|
|
|
cpBtn.addEventListener('click', () => {
|
2025-03-28 08:09:18 +01:00
|
|
|
navigator.clipboard.writeText(item.url)
|
2026-02-15 23:45:11 +01:00
|
|
|
.then(() => showNotification(t('shared_linkCopied', 'Link copied!')))
|
|
|
|
|
.catch(() => showNotification(t('shared_linkCopyFailed', 'Failed to copy link'), 'error'));
|
2025-03-28 08:09:18 +01:00
|
|
|
});
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
const rmBtn = document.createElement('button');
|
|
|
|
|
rmBtn.className = 'action-btn remove-btn';
|
|
|
|
|
rmBtn.innerHTML = '<span class="action-icon">🗑️</span>';
|
|
|
|
|
rmBtn.title = t('shared_removeShare', 'Remove Share');
|
|
|
|
|
rmBtn.addEventListener('click', () => { currentSharedItem = item; removeSharedItem(); });
|
|
|
|
|
|
|
|
|
|
actionsCell.append(editBtn, notifyBtn, cpBtn, rmBtn);
|
|
|
|
|
row.append(nameCell, typeCell, dateCell, expirationCell, permissionsCell, passwordCell, actionsCell);
|
2025-03-28 08:09:18 +01:00
|
|
|
sharedItemsList.appendChild(row);
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
// ── Share dialog ──
|
2025-03-28 08:09:18 +01:00
|
|
|
function openShareDialog(item) {
|
|
|
|
|
currentSharedItem = item;
|
2026-02-15 23:45:11 +01:00
|
|
|
const dn = item.item_name || item.item_id || 'Unknown';
|
|
|
|
|
|
|
|
|
|
if (shareDialogIcon) shareDialogIcon.textContent = item.item_type === 'file' ? '📄' : '📁';
|
|
|
|
|
if (shareDialogName) shareDialogName.textContent = dn;
|
|
|
|
|
if (shareLinkUrl) shareLinkUrl.value = item.url || '';
|
|
|
|
|
|
|
|
|
|
if (permissionRead) permissionRead.checked = item.permissions?.read !== false;
|
|
|
|
|
if (permissionWrite) permissionWrite.checked = !!item.permissions?.write;
|
|
|
|
|
if (permissionReshare) permissionReshare.checked = !!item.permissions?.reshare;
|
|
|
|
|
|
|
|
|
|
if (enablePassword) {
|
|
|
|
|
enablePassword.checked = item.has_password;
|
|
|
|
|
if (sharePassword) { sharePassword.disabled = !enablePassword.checked; sharePassword.value = ''; }
|
|
|
|
|
}
|
|
|
|
|
if (enableExpiration) {
|
|
|
|
|
enableExpiration.checked = !!item.expires_at;
|
|
|
|
|
if (shareExpiration) {
|
|
|
|
|
shareExpiration.disabled = !enableExpiration.checked;
|
|
|
|
|
shareExpiration.value = item.expires_at ? new Date(item.expires_at * 1000).toISOString().split('T')[0] : '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (shareDialog) shareDialog.classList.add('active');
|
2025-03-28 08:09:18 +01:00
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
function closeShareDialog() {
|
2026-02-15 23:45:11 +01:00
|
|
|
if (shareDialog) shareDialog.classList.remove('active');
|
2025-03-28 08:09:18 +01:00
|
|
|
currentSharedItem = null;
|
|
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
// ── Notification dialog ──
|
2025-03-28 08:09:18 +01:00
|
|
|
function openNotificationDialog(item) {
|
|
|
|
|
currentSharedItem = item;
|
2026-02-15 23:45:11 +01:00
|
|
|
const dn = item.item_name || item.item_id || 'Unknown';
|
|
|
|
|
if (notifyDialogIcon) notifyDialogIcon.textContent = item.item_type === 'file' ? '📄' : '📁';
|
|
|
|
|
if (notifyDialogName) notifyDialogName.textContent = dn;
|
|
|
|
|
if (notificationEmail) notificationEmail.value = '';
|
|
|
|
|
if (notificationMessage) notificationMessage.value = '';
|
|
|
|
|
if (notificationDialog) notificationDialog.classList.add('active');
|
2025-03-28 08:09:18 +01:00
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
function closeNotificationDialog() {
|
2026-02-15 23:45:11 +01:00
|
|
|
if (notificationDialog) notificationDialog.classList.remove('active');
|
2025-03-28 08:09:18 +01:00
|
|
|
currentSharedItem = null;
|
|
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
2025-03-28 08:09:18 +01:00
|
|
|
function copyShareLink() {
|
2026-02-15 23:45:11 +01:00
|
|
|
if (!shareLinkUrl) return;
|
2025-03-28 08:09:18 +01:00
|
|
|
navigator.clipboard.writeText(shareLinkUrl.value)
|
2026-02-15 23:45:11 +01:00
|
|
|
.then(() => showNotification(t('shared_linkCopied', 'Link copied!')))
|
|
|
|
|
.catch(() => showNotification(t('shared_linkCopyFailed', 'Failed to copy link'), 'error'));
|
2025-03-28 08:09:18 +01:00
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
// ── Generate secure password with crypto API ──
|
2025-03-28 08:09:18 +01:00
|
|
|
function generatePassword() {
|
2026-02-15 23:45:11 +01:00
|
|
|
if (!sharePassword || !enablePassword) return;
|
2025-03-28 08:09:18 +01:00
|
|
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*';
|
2026-02-15 23:45:11 +01:00
|
|
|
const array = new Uint32Array(16);
|
|
|
|
|
crypto.getRandomValues(array);
|
2025-03-28 08:09:18 +01:00
|
|
|
let password = '';
|
2026-02-15 23:45:11 +01:00
|
|
|
for (let i = 0; i < 16; i++) {
|
|
|
|
|
password += chars[array[i] % chars.length];
|
2025-03-28 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
sharePassword.value = password;
|
|
|
|
|
enablePassword.checked = true;
|
|
|
|
|
sharePassword.disabled = false;
|
|
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
// ── Update share via API ──
|
|
|
|
|
async function updateSharedItem() {
|
2025-03-28 08:09:18 +01:00
|
|
|
if (!currentSharedItem) return;
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
|
permissions: {
|
|
|
|
|
read: permissionRead ? permissionRead.checked : true,
|
|
|
|
|
write: permissionWrite ? permissionWrite.checked : false,
|
|
|
|
|
reshare: permissionReshare ? permissionReshare.checked : false
|
|
|
|
|
},
|
|
|
|
|
password: (enablePassword && enablePassword.checked && sharePassword && sharePassword.value) ? sharePassword.value : null,
|
|
|
|
|
expires_at: (enableExpiration && enableExpiration.checked && shareExpiration && shareExpiration.value)
|
|
|
|
|
? Math.floor(new Date(shareExpiration.value).getTime() / 1000)
|
|
|
|
|
: null
|
2025-03-28 08:09:18 +01:00
|
|
|
};
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch(`/api/shares/${currentSharedItem.id}`, {
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
headers: authHeaders(true),
|
|
|
|
|
body: JSON.stringify(body)
|
|
|
|
|
});
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
const err = await res.json().catch(() => ({}));
|
|
|
|
|
throw new Error(err.error || `Server error ${res.status}`);
|
|
|
|
|
}
|
|
|
|
|
showNotification(t('shared_itemUpdated', 'Share settings updated'));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Error updating share:', err);
|
|
|
|
|
showNotification(err.message || 'Error updating share', 'error');
|
|
|
|
|
}
|
2025-03-28 08:09:18 +01:00
|
|
|
closeShareDialog();
|
2026-02-15 23:45:11 +01:00
|
|
|
await loadSharedItems();
|
2025-03-28 08:09:18 +01:00
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
// ── Remove share via API ──
|
|
|
|
|
async function removeSharedItem() {
|
2025-03-28 08:09:18 +01:00
|
|
|
if (!currentSharedItem) return;
|
2026-02-15 23:45:11 +01:00
|
|
|
try {
|
|
|
|
|
const res = await fetch(`/api/shares/${currentSharedItem.id}`, {
|
|
|
|
|
method: 'DELETE',
|
|
|
|
|
headers: authHeaders()
|
|
|
|
|
});
|
|
|
|
|
if (!res.ok && res.status !== 204) throw new Error(`Server error ${res.status}`);
|
|
|
|
|
showNotification(t('shared_itemRemoved', 'Share removed'));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Error removing share:', err);
|
|
|
|
|
showNotification('Error removing share', 'error');
|
|
|
|
|
}
|
2025-03-28 08:09:18 +01:00
|
|
|
closeShareDialog();
|
2026-02-15 23:45:11 +01:00
|
|
|
await loadSharedItems();
|
2025-03-28 08:09:18 +01:00
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
// ── Send notification (stub) ──
|
2025-03-28 08:09:18 +01:00
|
|
|
function sendNotification() {
|
|
|
|
|
if (!currentSharedItem) return;
|
2026-02-15 23:45:11 +01:00
|
|
|
const email = notificationEmail ? notificationEmail.value.trim() : '';
|
|
|
|
|
const message = notificationMessage ? notificationMessage.value.trim() : '';
|
|
|
|
|
|
|
|
|
|
if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
|
|
|
|
showNotification(t('shared_invalidEmail', 'Please enter a valid email address'), 'error');
|
2025-03-28 08:09:18 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
if (window.fileSharing && window.fileSharing.sendShareNotification) {
|
|
|
|
|
window.fileSharing.sendShareNotification(currentSharedItem.url, email, message)
|
|
|
|
|
.then(() => { closeNotificationDialog(); showNotification(t('shared_notificationSent', 'Notification sent')); })
|
|
|
|
|
.catch(() => showNotification(t('shared_notificationFailed', 'Failed to send notification'), 'error'));
|
|
|
|
|
}
|
2025-03-28 08:09:18 +01:00
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
// ── Helpers ──
|
2025-03-28 08:09:18 +01:00
|
|
|
function showNotification(message, type = 'success') {
|
2026-02-15 23:45:11 +01:00
|
|
|
if (notificationBannerMessage && notificationBanner) {
|
|
|
|
|
notificationBannerMessage.textContent = message;
|
|
|
|
|
notificationBanner.className = 'notification-banner active ' + type;
|
|
|
|
|
setTimeout(() => notificationBanner.classList.remove('active'), 5000);
|
|
|
|
|
} else if (window.ui && window.ui.showNotification) {
|
|
|
|
|
window.ui.showNotification(message, type);
|
|
|
|
|
} else {
|
|
|
|
|
alert(message);
|
|
|
|
|
}
|
2025-03-28 08:09:18 +01:00
|
|
|
}
|
2026-02-15 23:45:11 +01:00
|
|
|
|
|
|
|
|
function formatDate(value) {
|
|
|
|
|
if (!value) return 'N/A';
|
|
|
|
|
const date = typeof value === 'number' ? new Date(value * 1000) : new Date(value);
|
|
|
|
|
return date.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' });
|
2025-03-28 08:09:18 +01:00
|
|
|
}
|
|
|
|
|
});
|