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-31 07:09:11 +01:00
|
|
|
const res = yield fetch(process.env.IMMICH_URL + '/api' + endpoint);
|
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-30 17:59:14 +01:00
|
|
|
else {
|
|
|
|
(0, index_1.log)('Immich API status ' + res.status);
|
|
|
|
console.log(yield res.text());
|
|
|
|
}
|
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-28 09:56:11 +01:00
|
|
|
getShareByKey(key) {
|
|
|
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
2024-10-30 20:31:41 +01:00
|
|
|
const link = (yield this.request('/shared-links/me?key=' + encodeURIComponent(key)));
|
2024-10-29 19:22:18 +01:00
|
|
|
if (link) {
|
|
|
|
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 {
|
2024-10-30 19:53:09 +01:00
|
|
|
// Filter assets to exclude trashed assets
|
2024-10-30 20:21:12 +01:00
|
|
|
link.assets = link.assets.filter(asset => !asset.isTrashed);
|
|
|
|
link.assets.forEach(asset => { asset.key = key; });
|
2024-10-29 19:22:18 +01:00
|
|
|
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-30 20:21:12 +01:00
|
|
|
return this.request('/assets/' + encodeURIComponent(asset.id) + '/' + size + '?key=' + encodeURIComponent(asset.key));
|
2024-10-29 10:31:40 +01:00
|
|
|
case types_1.AssetType.video:
|
2024-10-30 20:21:12 +01:00
|
|
|
return this.request('/assets/' + encodeURIComponent(asset.id) + '/video/playback?key=' + encodeURIComponent(asset.key));
|
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-31 07:09:11 +01:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW1taWNoLmpzIiwic291cmNlUm9vdCI6IiIs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