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-30 14:43:26 +01:00
|
|
|
const index_1 = require("./index");
|
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.
|
2024-10-30 14:28:29 +01:00
|
|
|
*
|
2024-10-29 15:07:54 +01:00
|
|
|
* 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
|
2024-10-30 14:43:26 +01:00
|
|
|
(0, index_1.log)('Expired link ' + key);
|
2024-10-29 19:22:18 +01:00
|
|
|
}
|
|
|
|
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.
|
2024-10-30 14:28:29 +01:00
|
|
|
*
|
2024-10-29 15:07:54 +01:00
|
|
|
* 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;
|
2024-10-29 20:44:46 +01:00
|
|
|
return this.request('/assets/' + encodeURIComponent(asset.id) + '/' + size);
|
2024-10-29 10:31:40 +01:00
|
|
|
case types_1.AssetType.video:
|
2024-10-29 20:44:46 +01:00
|
|
|
return this.request('/assets/' + encodeURIComponent(asset.id) + '/video/playback');
|
2024-10-29 10:31:40 +01:00
|
|
|
}
|
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-30 14:43:26 +01:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW1taWNoLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL2ltbWljaC50cyJdLCJuYW1lcyI6W10sI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