Skip to content

Commit ad00932

Browse files
committed
feat: add viewer.html generator
1 parent e9948d1 commit ad00932

File tree

6 files changed

+343
-10
lines changed

6 files changed

+343
-10
lines changed

configs/ignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.pydio
22
index.html
3+
viewer.html
34
_/*

src/IndexTemplate.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fs from 'node:fs'
22
import { render } from 'ejs'
33
import type { Directory } from './DirectoryFormat.js'
44
import path from 'node:path'
5-
import moment from 'moment'
65

76
export class IndexTemplate {
87
private readonly template = fs
@@ -23,8 +22,7 @@ export class IndexTemplate {
2322
map.set(
2423
path.join(directory.fullname, 'index.html'),
2524
render(this.template, {
26-
directory,
27-
indexedAt: moment.utc().format('YYYY-MM-DD HH:mm:ss UTC')
25+
directory
2826
})
2927
)
3028

src/ViewerTemplate.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import fs from 'node:fs'
2+
import { render } from 'ejs'
3+
import type { Directory } from './DirectoryFormat.js'
4+
import path from 'node:path'
5+
6+
export class ViewerTemplate {
7+
private readonly template = fs
8+
.readFileSync('./templates/viewer.ejs')
9+
.toString()
10+
11+
public compileAll(rootDirectory: Directory): Map<string, string> {
12+
return this.compileOne(rootDirectory)
13+
}
14+
15+
public compileOne(directory: Directory): Map<string, string> {
16+
const childOutputs = [...directory.directories.values()]
17+
.map((v) => [...this.compileOne(v)])
18+
.flat()
19+
20+
const map = new Map<string, string>(childOutputs)
21+
const files = [...directory.files.values()].filter((v) => v.viewerAvailable)
22+
23+
for (const file of files) {
24+
map.set(
25+
path.join(directory.fullname, file.name, 'viewer.html'),
26+
render(this.template, {
27+
file
28+
})
29+
)
30+
}
31+
32+
return map
33+
}
34+
}

src/main.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { S3Service } from './S3Service.js'
33
import { DirectoryFormat } from './DirectoryFormat.js'
44
import { IndexTemplate } from './IndexTemplate.js'
55
import { AssetsManager } from './AssetsManager.js'
6+
import { ViewerTemplate } from './ViewerTemplate.js'
67

78
class Main {
89
private readonly s3 = new S3Service()
910
private readonly formatter = new DirectoryFormat()
10-
private readonly template = new IndexTemplate()
11+
private readonly indexTemplate = new IndexTemplate()
12+
private readonly viewerTemplate = new ViewerTemplate()
1113
private readonly assets = new AssetsManager()
1214

1315
private isProcessing = false
@@ -38,9 +40,11 @@ class Main {
3840
private async runJob(): Promise<void> {
3941
const objects = await this.s3.getAllObjects()
4042
const directory = this.formatter.parse(objects)
41-
const indexes = this.template.compileAll(directory)
43+
const indexes = this.indexTemplate.compileAll(directory)
44+
const viewers = this.viewerTemplate.compileAll(directory)
45+
const allTextObjects = new Map<string, string>([...indexes, ...viewers])
4246

43-
await this.s3.putAllTextObjects(indexes)
47+
await this.s3.putAllTextObjects(allTextObjects)
4448

4549
console.log('Finished Cycle', new Date())
4650
}

templates/index.ejs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<meta name="description" content="Contents of /<%= directory.fullname %> - @pmh-only's public dumps">
6+
<meta name="description" content="Contents of /<%= directory.fullname %>">
77

88
<link rel="shortcut icon" href="/_/Niko_origin.webp" type="image/webp">
99
<link rel="preconnect" href="https://fonts.googleapis.com">
1010
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
1111
<link rel="preload" as="style" href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&family=Noto+Emoji:wght@700&display=swap" onload="this.onload=null;this.rel='stylesheet'">
1212

13-
<title>Contents of /<%= directory.fullname %> - public dumps</title>
13+
<title>Contents of /<%= directory.fullname %></title>
1414
</head>
1515
<body>
1616
<div class="container">
@@ -71,7 +71,7 @@
7171
<tr>
7272
<td>📄</td>
7373
<% if (file.viewerAvailable) { %>
74-
<td class="nav-col"><div><a href="/_/viewer.html#<%= Buffer.from(JSON.stringify(file)).toString('base64') %>"><%= file.name %></a></div></td>
74+
<td class="nav-col"><div><a href="/<%= file.fullname %>/viewer.html"><%= file.name %></a></div></td>
7575
<% } else { %>
7676
<td class="nav-col"><div><a href="/<%= file.fullname %>"><%= file.name %></a></div></td>
7777
<% } %>
@@ -84,7 +84,7 @@
8484
</table>
8585
</div>
8686

87-
<img id="niko" src="/_/Niko_origin.webp" alt="Niko The Cat">
87+
<img id="niko" src="/_/Niko_origin.webp" alt="Niko the Cat">
8888

8989
<style>
9090
:root {

0 commit comments

Comments
 (0)