-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #635 from danactive/add-age
Display persons in album w/ Age
- Loading branch information
Showing
22 changed files
with
1,886 additions
and
2,796 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<persons> | ||
<person first="Mister" last="Gingerbread" dob="2004-01-02" /> | ||
<person first="Missus" last="Gingerbread" dob="2004-01-02" /> | ||
</persons> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import transformJsonSchema, { errorSchema, type ErrorOptionalMessage } from '../models/person' | ||
import type { AlbumMeta, Person } from '../types/common' | ||
import { getPersonsFromFilesystem } from './xml' | ||
|
||
type Envelope = { body: Person[], status: number } | ||
type ErrorOptionalMessageBody = { | ||
body: ErrorOptionalMessage; status: number; | ||
} | ||
type ReturnAlbumOrErrors = Promise<Envelope | Person[] | ErrorOptionalMessage | ErrorOptionalMessageBody> | ||
async function get<T extends boolean = false>( | ||
gallery: AlbumMeta['gallery'], | ||
returnEnvelope?: T, | ||
): Promise<T extends true ? Envelope : Person[]> | ||
/** | ||
* Get Persons XML from local filesystem | ||
* @param {string} gallery name of gallery | ||
* @param {boolean} returnEnvelope will enable a return value with HTTP status code and body | ||
* @returns {object} person | ||
*/ | ||
async function get( | ||
gallery: AlbumMeta['gallery'], | ||
returnEnvelope: boolean, | ||
): ReturnAlbumOrErrors { | ||
try { | ||
if (gallery === null || gallery === undefined) { | ||
throw new ReferenceError('Gallery name is missing') | ||
} | ||
const json = await getPersonsFromFilesystem(gallery) | ||
const body = transformJsonSchema(json) | ||
|
||
if (returnEnvelope) { | ||
return { body, status: 200 } | ||
} | ||
|
||
return body | ||
} catch (e) { | ||
const message = `No person file was found; gallery=${gallery};` | ||
if (returnEnvelope) { | ||
return { body: errorSchema(message), status: 404 } | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.error('ERROR', message, e) | ||
throw e | ||
} | ||
} | ||
|
||
export default get |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import camelCase from 'camelcase' | ||
import fs from 'node:fs/promises' | ||
import xml2js, { type ParserOptions } from 'xml2js' | ||
|
||
import type { | ||
AlbumMeta, XmlAlbum, XmlGallery, XmlPersons, | ||
} from '../types/common' | ||
|
||
const parseOptions: ParserOptions = { explicitArray: false, normalizeTags: true, tagNameProcessors: [(name) => camelCase(name)] } | ||
const parser = new xml2js.Parser(parseOptions) | ||
|
||
/** | ||
* Get Album XML from local filesystem | ||
* @param {string} gallery name of gallery | ||
* @param {string} album name of album | ||
* @returns {string} album XML | ||
*/ | ||
async function readAlbum(gallery: NonNullable<AlbumMeta['gallery']>, album: string): Promise<XmlAlbum> { | ||
const fileBuffer = await fs.readFile(`public/galleries/${gallery}/${album}.xml`) | ||
return parser.parseStringPromise(fileBuffer) | ||
} | ||
|
||
/** | ||
* Get Gallery XML from local filesystem | ||
* @param {string} gallery name of gallery | ||
* @returns {string} album as JSON | ||
*/ | ||
async function readGallery(gallery: NonNullable<AlbumMeta['gallery']>): Promise<XmlGallery> { | ||
const fileBuffer = await fs.readFile(`public/galleries/${gallery}/gallery.xml`) | ||
return parser.parseStringPromise(fileBuffer) | ||
} | ||
|
||
/** | ||
* Get Persons XML from local filesystem | ||
* @param {string} gallery name of gallery | ||
* @returns {string} album as JSON | ||
*/ | ||
async function readPersons(gallery: NonNullable<AlbumMeta['gallery']>): Promise<XmlPersons> { | ||
const fileBuffer = await fs.readFile(`public/galleries/${gallery}/persons.xml`) | ||
return parser.parseStringPromise(fileBuffer) | ||
} | ||
|
||
export { | ||
readAlbum as getAlbumFromFilesystem, | ||
readGallery as getGalleryFromFilesystem, | ||
readPersons as getPersonsFromFilesystem, | ||
} |
Oops, something went wrong.