rimgo/src/handlers.ts

63 lines
1.8 KiB
TypeScript
Raw Normal View History

import Hapi = require('@hapi/hapi');
import '@hapi/vision';
2021-10-06 17:47:10 +00:00
import { fetchAlbum, fetchAlbumURL, fetchComments, fetchGallery, fetchMedia } from './fetchers';
import * as util from './util';
import CONFIG from './config';
export const handleMedia = async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
const {
baseName,
extension,
} = request.params;
const result = await fetchMedia(`${baseName}.${extension}`);
const response = h.response(result.rawBody)
.header('Content-Type', result.headers["content-type"] || `image/${extension}`);
return response;
};
export const handleAlbum = async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
// https://imgur.com/a/DfEsrAB
2021-10-06 17:47:10 +00:00
const albumID = request.params.albumID;
if (CONFIG.disable_comments) {
const url = await fetchAlbumURL(albumID);
return h.view('bare-album', {
url,
pageTitle: CONFIG.page_title,
util,
});
} else {
const album = await fetchAlbum(albumID);
return h.view('gallery', {
...album,
pageTitle: CONFIG.page_title,
util,
});
}
};
export const handleUser = (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
// https://imgur.com/user/MomBotNumber5
throw new Error('not implemented');
};
export const handleTag = (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
// https://imgur.com/t/funny
throw new Error('not implemented');
};
export const handleGallery = async (request: Hapi.Request, h: Hapi.ResponseToolkit) => {
const galleryID = request.params.galleryID;
const gallery = await fetchGallery(galleryID);
const comments = CONFIG.disable_comments
? null
: await fetchComments(galleryID);
return h.view('gallery', {
...gallery,
comments,
2021-10-06 17:47:10 +00:00
pageTitle: CONFIG.page_title,
util,
});
};