Добавлена «Отправить через» в Android

This commit is contained in:
AidarKC
2026-11-28 02:02:28 +03:00
parent 34e5fa1403
commit ebd5d37039
20 changed files with 426 additions and 31 deletions
+62
View File
@@ -0,0 +1,62 @@
// Shared by the service worker and the window. No content is sent over the network.
const DB = 'shine-share-inbox-v1';
const STORE = 'items';
const TTL = 24 * 60 * 60 * 1000;
export const MAX_SHARE_FILES = 10;
export const MAX_SHARE_BYTES = 500 * 1024 * 1024;
export function shareText({ title = '', text = '', url = '' } = {}) {
const body = String(text).trim();
const link = String(url).trim();
const heading = String(title).trim();
const result = [body, link && !body.includes(link) ? link : ''].filter(Boolean).join('\n');
return [heading && !result.includes(heading) ? heading : '', result].filter(Boolean).join('\n');
}
function openDb() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(DB, 1);
request.onupgradeneeded = () => request.result.createObjectStore(STORE, { keyPath: 'id' });
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
async function transaction(mode, action) {
const db = await openDb();
try {
return await new Promise((resolve, reject) => {
const tx = db.transaction(STORE, mode);
let result;
tx.oncomplete = () => resolve(result);
tx.onabort = tx.onerror = () => reject(tx.error || new Error('share storage failed'));
action(tx.objectStore(STORE), value => { result = value; });
});
} finally { db.close(); }
}
export function pruneShares(now = Date.now()) {
return transaction('readwrite', store => {
const cursor = store.openCursor();
cursor.onsuccess = () => {
const row = cursor.result;
if (!row) return;
if (row.value.createdAt < now - TTL) row.delete();
row.continue();
};
});
}
export async function saveShare(form) {
const files = form.getAll('files').filter(file => typeof file !== 'string' && (file.name || file.size));
if (files.length > MAX_SHARE_FILES || files.reduce((total, file) => total + file.size, 0) > MAX_SHARE_BYTES) throw new Error('limit');
const text = shareText({ title: form.get('title') || '', text: form.get('text') || '', url: form.get('url') || '' });
if (!text && !files.length) throw new Error('empty');
const id = crypto.randomUUID();
await pruneShares();
await transaction('readwrite', store => store.put({ id, text, files, createdAt: Date.now() }));
return id;
}
export async function readShare(id) {
await pruneShares();
return transaction('readonly', (store, set) => {
const req = store.get(id);
req.onsuccess = () => set(req.result || null);
});
}
export const deleteShare = id => transaction('readwrite', store => store.delete(id));