@@ -2,6 +2,7 @@ import AWS from 'aws-sdk';
2
2
import fs from 'fs' ;
3
3
import nodePath from 'path' ;
4
4
import mime from 'mime-types' ;
5
+ import axios from 'axios' ;
5
6
import { backOff } from 'exponential-backoff' ;
6
7
7
8
const SortOrder = {
@@ -21,9 +22,18 @@ function sortFilesByDate(filesList, sortByDate = SortOrder.ASC) {
21
22
}
22
23
23
24
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
+ ) {
25
33
this . endpoint = endpoint ;
26
34
this . bucket = bucket ;
35
+ this . digitalOceanAPIToken = digitalOceanAPIToken ;
36
+ this . cdnEndpointId = cdnEndpointId ;
27
37
28
38
const options = {
29
39
endpoint,
@@ -68,7 +78,7 @@ class SpacesClient {
68
78
permission = 'private' ,
69
79
options = { } ,
70
80
) {
71
- const { exponentialBackoff = false , ...spacesOptions } = options ;
81
+ const { exponentialBackoff = false , purgeCache = false , ...spacesOptions } = options ;
72
82
const makeUpload = async ( ) => {
73
83
await this . s3client . upload ( {
74
84
Bucket : this . bucket ,
@@ -80,6 +90,19 @@ class SpacesClient {
80
90
...spacesOptions ,
81
91
} ) . promise ( ) ;
82
92
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
+
83
106
return this . getCDNURL ( destinationPath ) ;
84
107
} ;
85
108
0 commit comments