Skip to content

Commit 862e0be

Browse files
committed
Add purge cache option when uploading files
1 parent 2122b54 commit 862e0be

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"dependencies": {
5353
"@babel/runtime": "^7.8.7",
5454
"aws-sdk": "^2.641.0",
55+
"axios": "^0.19.2",
5556
"exponential-backoff": "^3.0.1",
5657
"mime-types": "^2.1.26"
5758
}

src/index.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import AWS from 'aws-sdk';
22
import fs from 'fs';
33
import nodePath from 'path';
44
import mime from 'mime-types';
5+
import axios from 'axios';
56
import { backOff } from 'exponential-backoff';
67

78
const SortOrder = {
@@ -21,9 +22,18 @@ function sortFilesByDate(filesList, sortByDate = SortOrder.ASC) {
2122
}
2223

2324
class SpacesClient {
24-
constructor(endpoint, bucket, accessKeyId = null, secretAccessKey = null) {
25+
constructor(
26+
endpoint,
27+
bucket,
28+
accessKeyId = null,
29+
secretAccessKey = null,
30+
digitalOceanAPIToken = null,
31+
cdnEndpointId = null,
32+
) {
2533
this.endpoint = endpoint;
2634
this.bucket = bucket;
35+
this.digitalOceanAPIToken = digitalOceanAPIToken;
36+
this.cdnEndpointId = cdnEndpointId;
2737

2838
const options = {
2939
endpoint,
@@ -68,7 +78,7 @@ class SpacesClient {
6878
permission = 'private',
6979
options = {},
7080
) {
71-
const { exponentialBackoff = false, ...spacesOptions } = options;
81+
const { exponentialBackoff = false, purgeCache = false, ...spacesOptions } = options;
7282
const makeUpload = async () => {
7383
await this.s3client.upload({
7484
Bucket: this.bucket,
@@ -80,6 +90,19 @@ class SpacesClient {
8090
...spacesOptions,
8191
}).promise();
8292

93+
if (purgeCache) {
94+
await axios.request({
95+
url: `https://api.digitalocean.com/v2/cdn/endpoints/${this.cdnEndpointId}/cache`,
96+
method: 'DELETE',
97+
headers: {
98+
Authorization: `Bearer ${this.digitalOceanAPIToken}`,
99+
},
100+
data: {
101+
files: [destinationPath],
102+
},
103+
});
104+
}
105+
83106
return this.getCDNURL(destinationPath);
84107
};
85108

0 commit comments

Comments
 (0)