mirror of
https://github.com/alangrainger/immich-public-proxy.git
synced 2024-12-29 12:21:57 +00:00
92 lines
No EOL
9.6 KiB
JavaScript
92 lines
No EOL
9.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const tslib_1 = require("tslib");
|
|
const types_1 = require("./types");
|
|
class Immich {
|
|
/**
|
|
* Make a request to Immich API. We're not using the SDK to limit
|
|
* the possible attack surface of this app.
|
|
*/
|
|
request(endpoint) {
|
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
const res = yield fetch(process.env.IMMICH_URL + '/api' + endpoint, {
|
|
headers: {
|
|
'x-api-key': process.env.API_KEY || ''
|
|
}
|
|
});
|
|
if (res.status === 200) {
|
|
const contentType = res.headers.get('Content-Type') || '';
|
|
if (contentType.includes('application/json')) {
|
|
return res.json();
|
|
}
|
|
else {
|
|
return res;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
/**
|
|
* 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.
|
|
*/
|
|
getShareByKey(key) {
|
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
const res = ((yield this.request('/shared-links')) || []);
|
|
return res === null || res === void 0 ? void 0 : res.find(x => x.key === key);
|
|
});
|
|
}
|
|
/**
|
|
* 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.
|
|
*/
|
|
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');
|
|
}
|
|
});
|
|
}
|
|
/**
|
|
* Get the content-type of an Immich asset
|
|
*/
|
|
getContentType(asset) {
|
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
const assetBuffer = yield this.getAssetBuffer(asset);
|
|
return assetBuffer.headers.get('Content-Type');
|
|
});
|
|
}
|
|
/**
|
|
* Return the image data URL for a photo
|
|
*/
|
|
photoUrl(key, id, size) {
|
|
return `${process.env.SERVER_URL}/photo/${key}/${id}` + (size ? `?size=${size}` : '');
|
|
}
|
|
/**
|
|
* Return the video data URL for a video
|
|
*/
|
|
videoUrl(key, id) {
|
|
return `${process.env.SERVER_URL}/video/${key}/${id}`;
|
|
}
|
|
/**
|
|
* Check if a provided ID matches the Immich ID format
|
|
*/
|
|
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}$/);
|
|
}
|
|
/**
|
|
* Check if a provided key matches the Immich shared-link key format
|
|
*/
|
|
isKey(key) {
|
|
return !!key.match(/^[\w-]+$/);
|
|
}
|
|
}
|
|
const immich = new Immich();
|
|
exports.default = immich;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW1taWNoLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL2ltbWljaC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFBQSxtQ0FBaUU7QUFFakUsTUFBTSxNQUFNO0lBQ1Y7OztPQUdHO0lBQ0csT0FBTyxDQUFFLFFBQWdCOztZQUM3QixNQUFNLEdBQUcsR0FBRyxNQUFNLEtBQUssQ0FBQyxPQUFPLENBQUMsR0FBRyxDQUFDLFVBQVUsR0FBRyxNQUFNLEdBQUcsUUFBUSxFQUFFO2dCQUNsRSxPQUFPLEVBQUU7b0JBQ1AsV0FBVyxFQUFFLE9BQU8sQ0FBQyxHQUFHLENBQUMsT0FBTyxJQUFJLEVBQ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