Convert to Typescript

This commit is contained in:
Alan Grainger
2024-10-28 09:56:11 +01:00
parent 94ebc61867
commit ca0eba5949
10 changed files with 3888 additions and 14 deletions
+8 -5
View File
@@ -1,8 +1,10 @@
import { SharedLink } from './types'
class Immich {
async request (endpoint, json = true) {
async request (endpoint: string, json = true) {
const res = await fetch(process.env.IMMICH_URL + '/api' + endpoint, {
headers: {
'x-api-key': process.env.API_KEY
'x-api-key': process.env.API_KEY || ''
}
})
if (json) {
@@ -12,16 +14,17 @@ class Immich {
}
}
async getShareByKey (key) {
const links = await this.request('/shared-links')
async getShareByKey (key: string) {
const links = await this.request('/shared-links') as SharedLink[]
return links.find(x => x.key === key)
}
async getImage (id, size) {
async getImage (id: string, size = 'original') {
size = size === 'thumbnail' ? 'thumbnail' : 'original'
return this.request('/assets/' + id + '/' + size, false)
}
}
const api = new Immich()
export default api
+7 -5
View File
@@ -1,13 +1,13 @@
const express = require('express')
const api = require('./immich').default
const dayjs = require('dayjs')
import express from 'express'
import api from './immich'
import dayjs from 'dayjs'
const app = express()
require('dotenv').config()
app.get('/share/:key', async (req, res) => {
if (req.params.key.match(/[^A-Za-z0-9\-]/)) {
if (req.params.key.match(/[^A-Za-z0-9-]/)) {
// Invalid characters in the incoming URL
res.status(404).send()
} else {
@@ -15,11 +15,13 @@ app.get('/share/:key', async (req, res) => {
if (share?.assets?.length === 1) {
// This is an individual item (not a gallery)
// Return the full-size image.
const image = await api.getImage(share.assets[0].id, req.query.size)
const size = req.query.size === 'thumbnail' ? 'thumbnail' : 'original'
const image = await api.getImage(share.assets[0].id, size)
for (const header of ['content-type', 'content-length']) {
res.set(header, image.headers[header])
}
const data = await image.arrayBuffer()
console.log(dayjs().format() + ' Serving image ' + share.assets[0].id)
res.send(Buffer.from(data))
} else {
res.send()
+8
View File
@@ -0,0 +1,8 @@
export interface SharedLinkAsset {
id: string;
}
export interface SharedLink {
key: string;
assets: SharedLinkAsset[]
}