Skip to content

Commit 24cc71b

Browse files
committed
feat: do not upload file when same etag object exists
1 parent 0c3a348 commit 24cc71b

File tree

2 files changed

+26
-29
lines changed

2 files changed

+26
-29
lines changed

src/S3Service.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '@aws-sdk/client-s3'
99
import mime from 'mime-types'
1010
import path from 'node:path'
11+
import { createHash } from 'crypto'
1112

1213
export interface S3Object extends _Object {
1314
ContentType?: string
@@ -74,39 +75,53 @@ export class S3Service {
7475

7576
public async putAllTextObjects(objects: Map<string, string>): Promise<void> {
7677
const jobs = [...objects.entries()].map(
77-
async ([Key, Body]) =>
78-
await new Promise((resolve) => {
78+
([Key, Body]) =>
79+
new Promise((resolve, reject) => {
7980
const command = new PutObjectCommand({
8081
Bucket: process.env.S3_BUCKET_NAME,
8182
Key,
8283
Body,
83-
ContentType: 'text/html'
84+
ContentType: 'text/html',
85+
IfNoneMatch: this.md5Quoted(Body)
8486
})
8587

86-
this.client.send(command).then(resolve.bind(this))
88+
this.client
89+
.send(command)
90+
.then(resolve.bind(this))
91+
.catch(reject.bind(this))
8792
})
8893
)
8994

90-
await Promise.all(jobs)
95+
await Promise.allSettled(jobs)
9196
}
9297

9398
public async putAllBinaryObjects(objects: Map<string, string>) {
9499
const jobs = [...objects.entries()].map(
95-
async ([Key, filepath]) =>
96-
await new Promise((resolve) => {
100+
([Key, filepath]) =>
101+
new Promise((resolve, reject) => {
102+
const Body = fs.readFileSync(filepath)
97103
const command = new PutObjectCommand({
98104
Bucket: process.env.S3_BUCKET_NAME,
99105
Key,
100-
Body: fs.readFileSync(filepath),
106+
Body,
107+
IfNoneMatch: this.md5Quoted(Body),
101108
ContentType:
102109
mime.contentType(path.extname(filepath)) ||
103110
'application/octet-stream'
104111
})
105112

106-
this.client.send(command).then(resolve.bind(this))
113+
this.client
114+
.send(command)
115+
.then(resolve.bind(this))
116+
.catch(reject.bind(this))
107117
})
108118
)
109119

110-
await Promise.all(jobs)
120+
await Promise.allSettled(jobs)
121+
}
122+
123+
private md5Quoted(body: string | Buffer): string {
124+
const hash = createHash('md5').update(body).digest('hex')
125+
return `"${hash}"`
111126
}
112127
}

templates/index.ejs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@
8282
<% } %>
8383
</tbody>
8484
</table>
85-
86-
<footer>
87-
<p><a href="https://github.com/pmh-only/indexgen">indexgen</a> | indexed at <%= indexedAt %></p>
88-
</footer>
8985
</div>
9086

9187
<img id="niko" src="/_/Niko_origin.webp" alt="Niko The Cat">
@@ -257,20 +253,6 @@
257253
table tr td:nth-child(5), table tr td:nth-child(5) div {
258254
width: 240px;
259255
}
260-
261-
footer {
262-
font-size: 0.9rem;
263-
color: var(--text-color);
264-
}
265-
266-
footer a {
267-
color: var(--primary-color);
268-
text-decoration: none;
269-
}
270-
271-
footer a:hover {
272-
text-decoration: underline;
273-
}
274256
275257
@media (max-width: 980px) {
276258
.hide-when-mobile {
@@ -304,7 +286,7 @@
304286
bottom: 0px;
305287
right: 0px;
306288
opacity: 0.7;
307-
width: 150px;
289+
width: 100px;
308290
pointer-events: none;
309291
}
310292
</style>

0 commit comments

Comments
 (0)