2024-10-28 09:56:11 +01:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const tslib_1 = require("tslib");
|
2024-10-29 10:31:40 +01:00
|
|
|
const types_1 = require("./types");
|
2024-10-29 19:22:18 +01:00
|
|
|
const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
|
2024-10-28 09:56:11 +01:00
|
|
|
class Immich {
|
2024-10-29 15:07:54 +01:00
|
|
|
/**
|
|
|
|
* Make a request to Immich API. We're not using the SDK to limit
|
|
|
|
* the possible attack surface of this app.
|
|
|
|
*/
|
2024-10-29 10:31:40 +01:00
|
|
|
request(endpoint) {
|
|
|
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
2024-10-28 09:56:11 +01:00
|
|
|
const res = yield fetch(process.env.IMMICH_URL + '/api' + endpoint, {
|
|
|
|
headers: {
|
|
|
|
'x-api-key': process.env.API_KEY || ''
|
|
|
|
}
|
|
|
|
});
|
2024-10-29 10:31:40 +01:00
|
|
|
if (res.status === 200) {
|
|
|
|
const contentType = res.headers.get('Content-Type') || '';
|
|
|
|
if (contentType.includes('application/json')) {
|
|
|
|
return res.json();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return res;
|
|
|
|
}
|
2024-10-28 09:56:11 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-10-29 15:07:54 +01:00
|
|
|
/**
|
|
|
|
* Query Immich for the SharedLink metadata for a given key.
|
|
|
|
* The key is what is returned in the URL when you create a share in Immich.
|
|
|
|
* Immich doesn't have a method to query by key, so this method gets all
|
|
|
|
* known shared links, and returns the link which matches the provided key.
|
|
|
|
*/
|
2024-10-28 09:56:11 +01:00
|
|
|
getShareByKey(key) {
|
|
|
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
2024-10-29 10:31:40 +01:00
|
|
|
const res = ((yield this.request('/shared-links')) || []);
|
2024-10-29 19:22:18 +01:00
|
|
|
const link = res.find(x => x.key === key);
|
|
|
|
if (link) {
|
|
|
|
// Filter assets to exclude trashed assets
|
|
|
|
link.assets = link.assets.filter(x => !x.isTrashed);
|
|
|
|
if (link.expiresAt && (0, dayjs_1.default)(link.expiresAt) < (0, dayjs_1.default)()) {
|
|
|
|
// This link has expired
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
}
|
2024-10-29 10:31:40 +01:00
|
|
|
});
|
|
|
|
}
|
2024-10-29 15:07:54 +01:00
|
|
|
/**
|
|
|
|
* Stream asset buffer data from Immich.
|
|
|
|
* For photos, you can request 'thumbnail' or 'original' size.
|
|
|
|
* For videos, it is Immich's streaming quality, not the original quality.
|
|
|
|
*/
|
2024-10-29 10:31:40 +01:00
|
|
|
getAssetBuffer(asset, size) {
|
|
|
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
|
|
switch (asset.type) {
|
|
|
|
case types_1.AssetType.image:
|
|
|
|
size = size === types_1.ImageSize.thumbnail ? types_1.ImageSize.thumbnail : types_1.ImageSize.original;
|
|
|
|
return this.request('/assets/' + asset.id + '/' + size);
|
|
|
|
case types_1.AssetType.video:
|
|
|
|
return this.request('/assets/' + asset.id + '/video/playback');
|
|
|
|
}
|
2024-10-28 09:56:11 +01:00
|
|
|
});
|
|
|
|
}
|
2024-10-29 15:07:54 +01:00
|
|
|
/**
|
|
|
|
* Get the content-type of an Immich asset
|
|
|
|
*/
|
2024-10-29 10:31:40 +01:00
|
|
|
getContentType(asset) {
|
|
|
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
|
|
const assetBuffer = yield this.getAssetBuffer(asset);
|
|
|
|
return assetBuffer.headers.get('Content-Type');
|
2024-10-28 09:56:11 +01:00
|
|
|
});
|
|
|
|
}
|
2024-10-29 15:07:54 +01:00
|
|
|
/**
|
|
|
|
* Return the image data URL for a photo
|
|
|
|
*/
|
|
|
|
photoUrl(key, id, size) {
|
2024-10-29 19:57:44 +01:00
|
|
|
return `/photo/${key}/${id}` + (size ? `?size=${size}` : '');
|
2024-10-29 10:31:40 +01:00
|
|
|
}
|
2024-10-29 15:07:54 +01:00
|
|
|
/**
|
|
|
|
* Return the video data URL for a video
|
|
|
|
*/
|
|
|
|
videoUrl(key, id) {
|
2024-10-29 19:57:44 +01:00
|
|
|
return `/video/${key}/${id}`;
|
2024-10-29 10:31:40 +01:00
|
|
|
}
|
2024-10-29 15:07:54 +01:00
|
|
|
/**
|
|
|
|
* Check if a provided ID matches the Immich ID format
|
|
|
|
*/
|
2024-10-29 10:31:40 +01:00
|
|
|
isId(id) {
|
|
|
|
return !!id.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);
|
|
|
|
}
|
2024-10-29 15:07:54 +01:00
|
|
|
/**
|
|
|
|
* Check if a provided key matches the Immich shared-link key format
|
|
|
|
*/
|
|
|
|
isKey(key) {
|
|
|
|
return !!key.match(/^[\w-]+$/);
|
|
|
|
}
|
2024-10-28 09:56:11 +01:00
|
|
|
}
|
2024-10-29 10:31:40 +01:00
|
|
|
const immich = new Immich();
|
|
|
|
exports.default = immich;
|
2024-10-29 19:57:44 +01:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW1taWNoLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL2ltbWljaC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFBQSxtQ0FBaUU7QUFDakUsMERBQXlCO0FBRXpCLE1BQU0sTUFBTTtJQUNWOzs7T0FHRztJQUNHLE9BQU8sQ0FBRSxRQUFnQjs7WUFDN0IsTUFBTSxHQUFHLEdBQUcsTUFBTSxLQUFLLENBQUMsT0FBTyxDQUFDLEdBQUcsQ0FBQyxVQUFVLEdBQUcsTUFBTSxHQUFHLFFBQVEsRUFBRTtnQkFDbEUsT0FBTyxFQUFFO29CQUNQLFdBQVcsRUFBRSxPQUFPLENBQU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