Skip to content

Commit

Permalink
feat: add custom cdn endpoint configuration for s3 bucket on digital …
Browse files Browse the repository at this point in the history
…ocean

closes #18 077e473
  • Loading branch information
0-vortex committed Apr 8, 2023
1 parent 158c7dd commit 0caf64f
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 174 deletions.
11 changes: 8 additions & 3 deletions changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ <h1 id="📦-open-saucedopengraphopensaucedpizza-changelog">📦 open-sauced/ope
<blockquote>
<p>All notable changes to this project will be documented in this file</p>
</blockquote>
<h2 id="120-beta3-2023-04-05"><a href="https://github.com/open-sauced/opengraph.opensauced.pizza/compare/v1.2.0-beta.2...v1.2.0-beta.3">1.2.0-beta.3</a> (2023-04-05)</h2>
<h3 id="🍕-features">🍕 Features</h3>
<ul>
<li>implement digital ocean spaces s3 client storage and cache images 3 days (<a href="https://github.com/open-sauced/opengraph.opensauced.pizza/commit/792992e8ad8849a8e1c10178b84e0caa4d023b1b">792992e</a>)</li>
</ul>
<h2 id="120-beta2-2023-04-04"><a href="https://github.com/open-sauced/opengraph.opensauced.pizza/compare/v1.2.0-beta.1...v1.2.0-beta.2">1.2.0-beta.2</a> (2023-04-04)</h2>
<h3 id="🔁-continuous-integration">🔁 Continuous Integration</h3>
<ul>
<li>correct live release url in github actions deployment (<a href="https://github.com/open-sauced/opengraph.opensauced.pizza/commit/4a434e675198be1ee3ec0000a85a02dccf1b080e">4a434e6</a>)</li>
</ul>
<h2 id="120-beta1-2023-04-04"><a href="https://github.com/open-sauced/opengraph.opensauced.pizza/compare/v1.1.2...v1.2.0-beta.1">1.2.0-beta.1</a> (2023-04-04)</h2>
<h3 id="🍕-features">🍕 Features</h3>
<h3 id="🍕-features-1">🍕 Features</h3>
<ul>
<li>add boilerplate s3-client sdk code connected to digital ocean spaces (<a href="https://github.com/open-sauced/opengraph.opensauced.pizza/commit/a4a71e349579e86be65ce201f2ec941177a1e3de">a4a71e3</a>)</li>
</ul>
Expand All @@ -66,7 +71,7 @@ <h3 id="🤖-build-system">🤖 Build System</h3>
<ul>
<li>remove extraneous colors package (<a href="https://github.com/open-sauced/opengraph.opensauced.pizza/commit/4c7d29aa9ad9a7fe79c30fb79ed3e57b971fa9a5">4c7d29a</a>)</li>
</ul>
<h3 id="🍕-features-1">🍕 Features</h3>
<h3 id="🍕-features-2">🍕 Features</h3>
<ul>
<li>add correct languages calculation and display (<a href="https://github.com/open-sauced/opengraph.opensauced.pizza/commit/977df7589d67e6987cd9cb4e6528a86f46470541">977df75</a>)</li>
</ul>
Expand Down Expand Up @@ -110,7 +115,7 @@ <h3 id="🧑💻-code-refactoring">🧑‍💻 Code Refactoring</h3>
<li>move to nextjs backend architecture static component functions and boilerplate (<a href="https://github.com/open-sauced/opengraph.opensauced.pizza/commit/d4f854d4d5cee998e6495753a44dc006547a32bd">d4f854d</a>)</li>
<li>rewrite code and correct buffers (<a href="https://github.com/open-sauced/opengraph.opensauced.pizza/commit/c12099a219b529250ba6fb8527911c4972d344d4">c12099a</a>)</li>
</ul>
<h3 id="🍕-features-2">🍕 Features</h3>
<h3 id="🍕-features-3">🍕 Features</h3>
<ul>
<li>add development tooling and backend configuration (<a href="https://github.com/open-sauced/opengraph.opensauced.pizza/commit/f1f5947c4b97829f8e17e56bd4c8c4aab5bd4c63">f1f5947</a>)</li>
<li>add dynamic image (<a href="https://github.com/open-sauced/opengraph.opensauced.pizza/commit/5797c5447edbe4dd1923fecf3af72c169cc7b781">5797c54</a>)</li>
Expand Down
240 changes: 76 additions & 164 deletions injectables/S3FileStorageService.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ <h6><b>Properties</b></h6>
<tr>
<td class="col-md-4">
<ul class="index-list">
<li>
<span class="modifier">Public</span>
<a href="#getCdnEndpoint" >getCdnEndpoint</a>
</li>
<li>
<span class="modifier">Public</span>
<a href="#getOriginEndpoint" >getOriginEndpoint</a>
</li>
<li>
<span class="modifier">Private</span>
<span class="modifier">Readonly</span>
Expand All @@ -98,12 +106,6 @@ <h6><b>Methods</b></h6>
<span class="modifier">Async</span>
<a href="#fileExists" >fileExists</a>
</li>
<li>
<a href="#generateFileUrl" >generateFileUrl</a>
</li>
<li>
<a href="#generateHash" >generateHash</a>
</li>
<li>
<span class="modifier">Async</span>
<a href="#getFileLastModified" >getFileLastModified</a>
Expand Down Expand Up @@ -135,7 +137,7 @@ <h3 id="constructor">Constructor</h3>
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="11" class="link-to-prism">src/s3-file-storage/s3-file-storage.service.ts:11</a></div>
<div class="io-line">Defined in <a href="" data-line="16" class="link-to-prism">src/s3-file-storage/s3-file-storage.service.ts:16</a></div>
</td>
</tr>

Expand Down Expand Up @@ -200,8 +202,8 @@ <h3 id="methods">

<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="32"
class="link-to-prism">src/s3-file-storage/s3-file-storage.service.ts:32</a></div>
<div class="io-line">Defined in <a href="" data-line="33"
class="link-to-prism">src/s3-file-storage/s3-file-storage.service.ts:33</a></div>
</td>
</tr>

Expand Down Expand Up @@ -249,144 +251,6 @@ <h3 id="methods">
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="generateFileUrl"></a>
<span class="name">
<span ><b>generateFileUrl</b></span>
<a href="#generateFileUrl"><span class="icon ion-ios-link"></span></a>
</span>
</td>
</tr>
<tr>
<td class="col-md-4">
<code>generateFileUrl(hash: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/string" target="_blank">string</a>)</code>
</td>
</tr>


<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="28"
class="link-to-prism">src/s3-file-storage/s3-file-storage.service.ts:28</a></div>
</td>
</tr>


<tr>
<td class="col-md-4">

<div class="io-description">
<b>Parameters :</b>

<table class="params">
<thead>
<tr>
<td>Name</td>
<td>Type</td>
<td>Optional</td>
</tr>
</thead>
<tbody>
<tr>
<td>hash</td>
<td>
<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/string" target="_blank" >string</a></code>
</td>

<td>
No
</td>


</tr>
</tbody>
</table>
</div>
<div>
</div>
<div class="io-description">
<b>Returns : </b> <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/string" target="_blank" >string</a></code>

</div>
<div class="io-description">

</div>
</td>
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="generateHash"></a>
<span class="name">
<span ><b>generateHash</b></span>
<a href="#generateHash"><span class="icon ion-ios-link"></span></a>
</span>
</td>
</tr>
<tr>
<td class="col-md-4">
<code>generateHash(content: Buffer)</code>
</td>
</tr>


<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="91"
class="link-to-prism">src/s3-file-storage/s3-file-storage.service.ts:91</a></div>
</td>
</tr>


<tr>
<td class="col-md-4">

<div class="io-description">
<b>Parameters :</b>

<table class="params">
<thead>
<tr>
<td>Name</td>
<td>Type</td>
<td>Optional</td>
</tr>
</thead>
<tbody>
<tr>
<td>content</td>
<td>
<code>Buffer</code>
</td>

<td>
No
</td>


</tr>
</tbody>
</table>
</div>
<div>
</div>
<div class="io-description">
<b>Returns : </b> <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/string" target="_blank" >string</a></code>

</div>
<div class="io-description">

</div>
</td>
</tr>
</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
Expand All @@ -409,8 +273,8 @@ <h3 id="methods">

<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="52"
class="link-to-prism">src/s3-file-storage/s3-file-storage.service.ts:52</a></div>
<div class="io-line">Defined in <a href="" data-line="53"
class="link-to-prism">src/s3-file-storage/s3-file-storage.service.ts:53</a></div>
</td>
</tr>

Expand Down Expand Up @@ -480,8 +344,8 @@ <h3 id="methods">

<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="75"
class="link-to-prism">src/s3-file-storage/s3-file-storage.service.ts:75</a></div>
<div class="io-line">Defined in <a href="" data-line="76"
class="link-to-prism">src/s3-file-storage/s3-file-storage.service.ts:76</a></div>
</td>
</tr>

Expand Down Expand Up @@ -559,6 +423,58 @@ <h3 id="methods">
<h3 id="inputs">
Properties
</h3>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="getCdnEndpoint"></a>
<span class="name">
<span class="modifier">Public</span>
<span ><b>getCdnEndpoint</b></span>
<a href="#getCdnEndpoint"><span class="icon ion-ios-link"></span></a>
</span>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Default value : </i><code>() &#x3D;&gt; {...}</code>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="14" class="link-to-prism">src/s3-file-storage/s3-file-storage.service.ts:14</a></div>
</td>
</tr>


</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
<td class="col-md-4">
<a name="getOriginEndpoint"></a>
<span class="name">
<span class="modifier">Public</span>
<span ><b>getOriginEndpoint</b></span>
<a href="#getOriginEndpoint"><span class="icon ion-ios-link"></span></a>
</span>
</td>
</tr>
<tr>
<td class="col-md-4">
<i>Default value : </i><code>() &#x3D;&gt; {...}</code>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="12" class="link-to-prism">src/s3-file-storage/s3-file-storage.service.ts:12</a></div>
</td>
</tr>


</tbody>
</table>
<table class="table table-sm table-bordered">
<tbody>
<tr>
Expand All @@ -580,7 +496,7 @@ <h3 id="inputs">
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="11" class="link-to-prism">src/s3-file-storage/s3-file-storage.service.ts:11</a></div>
<div class="io-line">Defined in <a href="" data-line="10" class="link-to-prism">src/s3-file-storage/s3-file-storage.service.ts:10</a></div>
</td>
</tr>

Expand All @@ -596,7 +512,6 @@ <h3 id="inputs">
<pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">import { Injectable, Inject } from &quot;@nestjs/common&quot;;
import { ConfigType } from &quot;@nestjs/config&quot;;
import { S3, HeadObjectCommand, PutObjectCommand, S3Client } from &quot;@aws-sdk/client-s3&quot;;
import { createHash } from &quot;node:crypto&quot;;
import { Readable } from &quot;node:stream&quot;;

import DigitalOceanConfig from &quot;../config/digital-ocean.config&quot;;
Expand All @@ -605,25 +520,27 @@ <h3 id="inputs">
export class S3FileStorageService {
private readonly s3Client: S3Client;

public getOriginEndpoint &#x3D; (): string &#x3D;&gt; &#x60;${this.config.protocol}://${this.config.bucketName}.${this.config.region}.${this.config.endpoint}/&#x60;;

public getCdnEndpoint &#x3D; (): string &#x3D;&gt; (this.config.cdnDisabled
? this.getOriginEndpoint()
: &#x60;${this.config.protocol}://${this.config.cdnCustomDomain !&#x3D;&#x3D; &quot;&quot; ? this.config.cdnCustomDomain : &#x60;${this.config.bucketName}.${this.config.region}.cdn.${this.config.endpoint}&#x60;}/&#x60;);

constructor (
@Inject(DigitalOceanConfig.KEY)
private readonly config: ConfigType&lt;typeof DigitalOceanConfig&gt;,
) {
this.s3Client &#x3D; new S3({
forcePathStyle: false,
endpoint: config.endpoint,
region: config.region,
endpoint: &#x60;${config.protocol}://${config.region}.${config.endpoint}&#x60;,
region: &#x60;us-east-1&#x60;,
credentials: {
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
},
});
}

generateFileUrl (hash: string): string {
return &#x60;https://${this.config.bucketName}.${this.config.endpoint.replace(/https?:\/\//, &quot;&quot;)}/${hash}&#x60;;
}

async fileExists (hash: string): Promise&lt;boolean&gt; {
try {
await this.s3Client.send(
Expand Down Expand Up @@ -682,11 +599,6 @@ <h3 id="inputs">
}),
);
}

generateHash (content: Buffer): string {
return createHash(&quot;sha256&quot;).update(content)
.digest(&quot;hex&quot;);
}
}
</code></pre>
</div>
Expand Down
2 changes: 1 addition & 1 deletion injectables/SocialCardService.html
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ <h3 id="inputs">

const { id, name, avatarUrl, repos, langs, langTotal } &#x3D; await this.getUserData(username);
const hash &#x3D; &#x60;users/${String(id)}.png&#x60;;
const fileUrl &#x3D; this.s3FileStorageService.generateFileUrl(hash);
const fileUrl &#x3D; &#x60;${this.s3FileStorageService.getCdnEndpoint()}${hash}&#x60;;
const hasFile &#x3D; await this.s3FileStorageService.fileExists(hash);
const today &#x3D; (new Date);
const today3daysAgo &#x3D; new Date((new Date).setDate(today.getDate() - 3));
Expand Down
4 changes: 2 additions & 2 deletions js/search/search_index.js

Large diffs are not rendered by default.

Loading

0 comments on commit 0caf64f

Please sign in to comment.