2023-05-09 03:34:24 +00:00
|
|
|
import axios from '@nextcloud/axios';
|
2023-05-10 20:26:01 +00:00
|
|
|
import { constants } from './services/Utils';
|
2023-05-18 06:57:13 +00:00
|
|
|
import { generateUrl } from '@nextcloud/router';
|
2023-08-20 20:00:41 +00:00
|
|
|
import type { IDay, IPhoto, IImageInfo } from './types';
|
2023-05-22 03:31:17 +00:00
|
|
|
const euc = encodeURIComponent;
|
2023-05-04 02:59:23 +00:00
|
|
|
|
2023-05-22 03:31:17 +00:00
|
|
|
/** Access NativeX over localhost */
|
2023-05-08 20:25:13 +00:00
|
|
|
const BASE_URL = 'http://127.0.0.1';
|
|
|
|
|
2023-08-20 20:00:41 +00:00
|
|
|
/** NativeX asynchronous API */
|
2023-05-08 20:25:13 +00:00
|
|
|
export const API = {
|
2023-08-20 20:00:41 +00:00
|
|
|
/**
|
|
|
|
* Local days API.
|
|
|
|
* @returns {IDay[]} for all locally available days.
|
|
|
|
*/
|
2023-05-08 20:25:13 +00:00
|
|
|
DAYS: () => `${BASE_URL}/api/days`,
|
2023-08-20 20:00:41 +00:00
|
|
|
/**
|
|
|
|
* Local photos API.
|
|
|
|
* @param dayId Day ID to fetch photos for
|
|
|
|
* @returns {IPhoto[]} for all locally available photos for this day.
|
|
|
|
*/
|
2023-05-08 20:25:13 +00:00
|
|
|
DAY: (dayId: number) => `${BASE_URL}/api/days/${dayId}`,
|
2023-08-20 20:00:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Local photo metadata API.
|
|
|
|
* @returns {IImageInfo} for the given file ID (local).
|
|
|
|
*/
|
2023-05-10 20:26:01 +00:00
|
|
|
IMAGE_INFO: (fileId: number) => `${BASE_URL}/api/image/info/${fileId}`,
|
2023-08-20 20:00:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete files using local fileids.
|
|
|
|
* @param fileIds Comma-separated list of file IDs to delete
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2023-05-10 20:26:01 +00:00
|
|
|
IMAGE_DELETE: (fileIds: number[]) => `${BASE_URL}/api/image/delete/${fileIds.join(',')}`,
|
2023-05-08 21:46:15 +00:00
|
|
|
|
2023-08-20 20:00:41 +00:00
|
|
|
/**
|
|
|
|
* Local photo preview API.
|
|
|
|
* @param fileId File ID of the photo
|
|
|
|
* @returns {Blob} JPEG preview of the photo.
|
|
|
|
*/
|
2023-05-08 20:25:13 +00:00
|
|
|
IMAGE_PREVIEW: (fileId: number) => `${BASE_URL}/image/preview/${fileId}`,
|
2023-08-20 20:00:41 +00:00
|
|
|
/**
|
|
|
|
* Local photo full API.
|
|
|
|
* @param fileId File ID of the photo
|
|
|
|
* @returns {Blob} JPEG full image of the photo.
|
|
|
|
*/
|
2023-05-08 20:25:13 +00:00
|
|
|
IMAGE_FULL: (fileId: number) => `${BASE_URL}/image/full/${fileId}`,
|
2023-05-11 03:02:52 +00:00
|
|
|
|
2023-08-20 20:00:41 +00:00
|
|
|
/**
|
|
|
|
* Share a URL with native page.
|
|
|
|
* The native client MUST NOT download the object but share the URL directly.
|
|
|
|
* @param url URL to share (double-encoded)
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2023-05-11 03:02:52 +00:00
|
|
|
SHARE_URL: (url: string) => `${BASE_URL}/api/share/url/${euc(euc(url))}`,
|
2023-08-20 20:00:41 +00:00
|
|
|
/**
|
|
|
|
* Share an object (as blob) natively using a given URL.
|
|
|
|
* The native client MUST download the object using a download manager
|
|
|
|
* and immediately prompt the user to download it. The asynchronous call
|
|
|
|
* must return only after the object has been downloaded.
|
|
|
|
* @param url URL to share (double-encoded)
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2023-05-11 03:02:52 +00:00
|
|
|
SHARE_BLOB: (url: string) => `${BASE_URL}/api/share/blob/${euc(euc(url))}`,
|
2023-08-20 20:00:41 +00:00
|
|
|
/**
|
|
|
|
* Share a local file (as blob) with native page.
|
|
|
|
* @param fileId File ID of the photo
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2023-05-11 03:24:53 +00:00
|
|
|
SHARE_LOCAL: (fileId: number) => `${BASE_URL}/api/share/local/${fileId}`,
|
2023-05-22 03:31:17 +00:00
|
|
|
|
2023-08-20 20:00:41 +00:00
|
|
|
/**
|
|
|
|
* Get list of local folders configuration.
|
|
|
|
* @returns {LocalFolderConfig[]} List of local folders configuration
|
|
|
|
*/
|
2023-05-22 03:31:17 +00:00
|
|
|
CONFIG_LOCAL_FOLDERS: () => `${BASE_URL}/api/config/local-folders`,
|
2023-05-08 20:25:13 +00:00
|
|
|
};
|
2023-05-04 02:59:23 +00:00
|
|
|
|
2023-08-20 20:00:41 +00:00
|
|
|
/** NativeX synchronous API. */
|
2023-05-04 02:59:23 +00:00
|
|
|
export type NativeX = {
|
2023-08-20 20:00:41 +00:00
|
|
|
/**
|
|
|
|
* Check if the native interface is available.
|
|
|
|
* @returns Should always return true.
|
|
|
|
*/
|
2023-05-04 02:59:23 +00:00
|
|
|
isNative: () => boolean;
|
2023-08-20 20:00:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the theme color of the app.
|
|
|
|
* @param color Color to set
|
|
|
|
* @param isDark Whether the theme is dark (for navigation bar)
|
|
|
|
*/
|
2023-05-08 04:06:30 +00:00
|
|
|
setThemeColor: (color: string, isDark: boolean) => void;
|
2023-08-20 20:00:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Play a tap sound for UI interaction.
|
|
|
|
*/
|
2023-05-15 00:26:14 +00:00
|
|
|
playTouchSound: () => void;
|
2023-05-14 20:59:00 +00:00
|
|
|
|
2023-08-20 20:00:41 +00:00
|
|
|
/**
|
|
|
|
* Start downloading a file from a given URL.
|
|
|
|
* @param url URL to download from
|
|
|
|
* @param filename Filename to save as
|
|
|
|
* @details An error must be shown to the user natively if the download fails.
|
|
|
|
*/
|
|
|
|
downloadFromUrl: (url: string, filename: string) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Play a video from the given file ID (local file).
|
|
|
|
* @param fileid File ID of the video
|
|
|
|
*/
|
2023-05-14 19:30:28 +00:00
|
|
|
playVideoLocal: (fileid: string) => void;
|
2023-08-20 20:00:41 +00:00
|
|
|
/**
|
|
|
|
* Play a video from the given URL(s).
|
|
|
|
* @param fileid Remote file ID of the video (used for play tracking)
|
|
|
|
* @param urlArray JSON-encoded array of URLs to play
|
|
|
|
* @details The URL array may contain multiple URLs, e.g. direct playback
|
|
|
|
* and HLS separately. The native client must try to play the first URL.
|
|
|
|
*/
|
2023-05-24 02:44:31 +00:00
|
|
|
playVideoRemote: (fileid: string, urlArray: string) => void;
|
2023-08-20 20:00:41 +00:00
|
|
|
/**
|
|
|
|
* Destroy the video player.
|
|
|
|
* @param fileid File ID of the video
|
|
|
|
* @details The native client must destroy the video player and free up resources.
|
|
|
|
* If the fileid doesn't match the playing video, the call must be ignored.
|
|
|
|
*/
|
2023-05-14 20:31:26 +00:00
|
|
|
destroyVideo: (fileid: string) => void;
|
2023-05-18 06:57:13 +00:00
|
|
|
|
2023-08-20 20:00:41 +00:00
|
|
|
/**
|
|
|
|
* Set the local folders configuration to show in the timeline.
|
|
|
|
* @param json JSON-encoded array of LocalFolderConfig
|
|
|
|
*/
|
2023-05-22 03:31:17 +00:00
|
|
|
configSetLocalFolders: (json: string) => void;
|
|
|
|
|
2023-08-20 20:00:41 +00:00
|
|
|
/**
|
|
|
|
* Log out from Nextcloud and delete the tokens.
|
|
|
|
*/
|
2023-05-18 06:57:13 +00:00
|
|
|
logout: () => void;
|
2023-05-04 02:59:23 +00:00
|
|
|
};
|
|
|
|
|
2023-05-22 03:31:17 +00:00
|
|
|
/** Setting of whether a local folder is enabled */
|
|
|
|
export type LocalFolderConfig = {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
enabled: boolean;
|
|
|
|
};
|
|
|
|
|
2023-05-04 02:59:23 +00:00
|
|
|
/** The native interface is a global object that is injected by the native app. */
|
|
|
|
const nativex: NativeX = globalThis.nativex;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns Whether the native interface is available.
|
|
|
|
*/
|
2023-05-11 03:02:52 +00:00
|
|
|
export function has() {
|
|
|
|
return !!nativex;
|
|
|
|
}
|
2023-05-04 02:59:23 +00:00
|
|
|
|
2023-05-08 04:06:30 +00:00
|
|
|
/**
|
2023-05-08 04:18:33 +00:00
|
|
|
* Change the theme color of the app to default.
|
2023-05-08 04:06:30 +00:00
|
|
|
*/
|
2023-05-11 03:02:52 +00:00
|
|
|
export async function setTheme(color?: string, dark?: boolean) {
|
2023-05-08 04:18:33 +00:00
|
|
|
if (!has()) return;
|
|
|
|
|
|
|
|
color ??= getComputedStyle(document.body).getPropertyValue('--color-main-background');
|
|
|
|
dark ??=
|
2023-05-19 02:18:04 +00:00
|
|
|
(document.body.hasAttribute('data-theme-default') && window.matchMedia('(prefers-color-scheme: dark)').matches) ||
|
2023-05-08 04:18:33 +00:00
|
|
|
document.body.hasAttribute('data-theme-dark') ||
|
|
|
|
document.body.hasAttribute('data-theme-dark-highcontrast');
|
|
|
|
nativex?.setThemeColor?.(color, dark);
|
2023-05-11 03:02:52 +00:00
|
|
|
}
|
2023-05-08 04:06:30 +00:00
|
|
|
|
2023-05-09 02:50:33 +00:00
|
|
|
/**
|
|
|
|
* Download a file from the given URL.
|
|
|
|
*/
|
2023-05-11 03:02:52 +00:00
|
|
|
export async function downloadFromUrl(url: string) {
|
2023-05-09 03:34:24 +00:00
|
|
|
// Make HEAD request to get filename
|
|
|
|
const res = await axios.head(url);
|
|
|
|
let filename = res.headers['content-disposition'];
|
|
|
|
if (res.status !== 200 || !filename) return;
|
|
|
|
|
|
|
|
// Extract filename from header without quotes
|
|
|
|
filename = filename.split('filename="')[1].slice(0, -1);
|
|
|
|
|
|
|
|
// Hand off to download manager
|
2023-05-11 03:02:52 +00:00
|
|
|
nativex?.downloadFromUrl?.(addOrigin(url), filename);
|
|
|
|
}
|
|
|
|
|
2023-05-15 00:26:14 +00:00
|
|
|
/**
|
|
|
|
* Play touch sound.
|
|
|
|
*/
|
|
|
|
export async function playTouchSound() {
|
|
|
|
nativex?.playTouchSound?.();
|
|
|
|
}
|
|
|
|
|
2023-05-14 19:30:28 +00:00
|
|
|
/**
|
|
|
|
* Play a video from the given file ID (local file).
|
|
|
|
*/
|
2023-05-14 20:59:00 +00:00
|
|
|
export async function playVideoLocal(fileid: number) {
|
|
|
|
nativex?.playVideoLocal?.(fileid.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-24 02:44:31 +00:00
|
|
|
* Play a video from the given URL.
|
2023-05-14 20:59:00 +00:00
|
|
|
*/
|
2023-05-24 02:44:31 +00:00
|
|
|
export async function playVideoRemote(fileid: number, urls: string[]) {
|
|
|
|
nativex?.playVideoRemote?.(fileid.toString(), JSON.stringify(urls.map(addOrigin)));
|
2023-05-14 19:30:28 +00:00
|
|
|
}
|
|
|
|
|
2023-05-14 20:31:26 +00:00
|
|
|
/**
|
|
|
|
* Destroy the video player.
|
|
|
|
*/
|
|
|
|
export async function destroyVideo(fileId: number) {
|
|
|
|
nativex?.destroyVideo?.(fileId.toString());
|
|
|
|
}
|
|
|
|
|
2023-05-11 03:02:52 +00:00
|
|
|
/**
|
|
|
|
* Share a URL with native page.
|
|
|
|
*/
|
|
|
|
export async function shareUrl(url: string) {
|
|
|
|
await axios.get(API.SHARE_URL(addOrigin(url)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download a blob from the given URL and share it.
|
|
|
|
*/
|
|
|
|
export async function shareBlobFromUrl(url: string) {
|
2023-05-11 03:24:53 +00:00
|
|
|
if (url.startsWith(BASE_URL)) {
|
|
|
|
throw new Error('Cannot share localhost URL');
|
|
|
|
}
|
2023-05-11 03:02:52 +00:00
|
|
|
await axios.get(API.SHARE_BLOB(addOrigin(url)));
|
|
|
|
}
|
2023-05-09 02:50:33 +00:00
|
|
|
|
2023-05-11 03:24:53 +00:00
|
|
|
/**
|
|
|
|
* Share a local file with native page.
|
|
|
|
*/
|
|
|
|
export async function shareLocal(fileId: number) {
|
|
|
|
await axios.get(API.SHARE_LOCAL(fileId));
|
|
|
|
}
|
|
|
|
|
2023-05-08 03:02:14 +00:00
|
|
|
/**
|
|
|
|
* Extend a list of days with local days.
|
|
|
|
* Fetches the local days from the native interface.
|
|
|
|
*/
|
|
|
|
export async function extendDaysWithLocal(days: IDay[]) {
|
|
|
|
if (!has()) return;
|
|
|
|
|
|
|
|
// Query native part
|
2023-05-08 20:25:13 +00:00
|
|
|
const res = await fetch(API.DAYS());
|
|
|
|
if (!res.ok) return;
|
|
|
|
const local: IDay[] = await res.json();
|
2023-05-08 03:02:14 +00:00
|
|
|
const remoteMap = new Map(days.map((d) => [d.dayid, d]));
|
|
|
|
|
|
|
|
// Merge local days into remote days
|
|
|
|
for (const day of local) {
|
|
|
|
const remote = remoteMap.get(day.dayid);
|
|
|
|
if (remote) {
|
|
|
|
remote.count = Math.max(remote.count, day.count);
|
|
|
|
} else {
|
|
|
|
days.push(day);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: sort depends on view
|
|
|
|
// (but we show it for only timeline anyway for now)
|
|
|
|
days.sort((a, b) => b.dayid - a.dayid);
|
|
|
|
}
|
|
|
|
|
2023-05-04 02:59:23 +00:00
|
|
|
/**
|
|
|
|
* Extend a list of photos with local photos.
|
|
|
|
* Fetches the local photos from the native interface and filters out duplicates.
|
|
|
|
*
|
|
|
|
* @param dayId Day ID to append local photos to
|
|
|
|
* @param photos List of photos to append to (duplicates will not be added)
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export async function extendDayWithLocal(dayId: number, photos: IPhoto[]) {
|
|
|
|
if (!has()) return;
|
|
|
|
|
2023-05-08 20:25:13 +00:00
|
|
|
// Query native part
|
|
|
|
const res = await fetch(API.DAY(dayId));
|
|
|
|
if (!res.ok) return;
|
|
|
|
|
|
|
|
// Merge local photos into remote photos
|
|
|
|
const localPhotos: IPhoto[] = await res.json();
|
2023-05-04 02:59:23 +00:00
|
|
|
const photosSet = new Set(photos.map((p) => p.basename));
|
|
|
|
const localOnly = localPhotos.filter((p) => !photosSet.has(p.basename));
|
|
|
|
localOnly.forEach((p) => (p.islocal = true));
|
|
|
|
photos.push(...localOnly);
|
2023-05-09 04:30:45 +00:00
|
|
|
|
|
|
|
// Sort by datetaken
|
|
|
|
photos.sort((a, b) => (b.datetaken ?? 0) - (a.datetaken ?? 0));
|
2023-05-04 02:59:23 +00:00
|
|
|
}
|
2023-05-10 20:26:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Request deletion of local photos wherever available.
|
|
|
|
* @param photos List of photos to delete
|
|
|
|
* @returns List of photos that were deleted
|
|
|
|
* @throws If the request fails
|
|
|
|
*/
|
|
|
|
export async function deleteLocalPhotos(photos: IPhoto[]): Promise<IPhoto[]> {
|
|
|
|
if (!has()) return [];
|
|
|
|
|
|
|
|
const localPhotos = photos.filter((p) => p.flag & constants.c.FLAG_IS_LOCAL);
|
|
|
|
if (localPhotos.length > 0) {
|
|
|
|
const fileids = localPhotos.map((p) => p.fileid);
|
|
|
|
await axios.get(API.IMAGE_DELETE(fileids));
|
|
|
|
}
|
|
|
|
|
|
|
|
return localPhotos;
|
|
|
|
}
|
2023-05-11 03:02:52 +00:00
|
|
|
|
2023-05-22 03:31:17 +00:00
|
|
|
/**
|
|
|
|
* Get list of local folders configuration.
|
|
|
|
* Should be called only if NativeX is available.
|
|
|
|
*/
|
|
|
|
export async function getLocalFolders() {
|
|
|
|
return (await axios.get<LocalFolderConfig[]>(API.CONFIG_LOCAL_FOLDERS())).data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set list of local folders configuration.
|
|
|
|
*/
|
|
|
|
export async function setLocalFolders(config: LocalFolderConfig[]) {
|
|
|
|
nativex?.configSetLocalFolders(JSON.stringify(config));
|
|
|
|
}
|
|
|
|
|
2023-05-18 06:57:13 +00:00
|
|
|
/**
|
|
|
|
* Log out from Nextcloud and pass ahead.
|
|
|
|
*/
|
|
|
|
export async function logout() {
|
|
|
|
await axios.get(generateUrl('logout'));
|
|
|
|
if (!has()) window.location.reload();
|
|
|
|
nativex?.logout();
|
|
|
|
}
|
|
|
|
|
2023-05-11 03:02:52 +00:00
|
|
|
/**
|
|
|
|
* Add current origin to URL if doesn't have any protocol or origin.
|
|
|
|
*/
|
|
|
|
function addOrigin(url: string) {
|
|
|
|
return url.match(/^(https?:)?\/\//)
|
|
|
|
? url
|
|
|
|
: url.startsWith('/')
|
|
|
|
? `${location.origin}${url}`
|
|
|
|
: `${location.origin}/${url}`;
|
|
|
|
}
|