Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit 27552d0

Browse files
authored
Merge pull request #317 from gastrohero/feature/add-google-cloud-storage-cache
Added google cloud storage cache as caching option and changelog entry for this feature
2 parents 81b3b07 + 1b93c35 commit 27552d0

File tree

5 files changed

+527
-14
lines changed

5 files changed

+527
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- mage2vs import now has optional `--generate-unique-url-keys` parameter which defaults to `false` to enable/disable the url key generation with name and id for categories - @rain2o (#232)
1212
- Added eslint config from vue-storefront so we have the same config and in both repos typescript support - @resubaka (#320)
1313
- Added jest support - @resubaka (#321)
14+
- Added caching factory and action factory for the image endpoint. This gives the possibility to use other services for caching or image processing - @resubaka (#317, #315)
1415

1516
### Fixed
1617
- The `product.price_*` fields have been normalized with the backward compatibility support (see `config.tax.deprecatedPriceFieldsSupport` which is by default true) - @pkarw (#289)

config/default.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@
229229
"type": "file",
230230
"file": {
231231
"path": "/tmp/vue-storefront-api"
232+
},
233+
"google-cloud-storage": {
234+
"libraryOptions": {},
235+
"bucket": "",
236+
"prefix": "vue-storefront-api/image-cache"
232237
}
233238
},
234239
"action": {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"author": "Piotr Karwatka <pkarwatka@divante.pl>",
4646
"license": "MIT",
4747
"dependencies": {
48+
"@google-cloud/storage": "^3.0.3",
4849
"ajv": "^6.4.0",
4950
"ajv-keywords": "^3.4.0",
5051
"body-parser": "^1.18.2",
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import ImageCache from '../abstract'
2+
import { createHash } from 'crypto'
3+
import { Bucket, Storage } from '@google-cloud/storage'
4+
export default class GoogleCloudStorageImageCache extends ImageCache {
5+
private static storage: Storage
6+
private static bucket: Bucket
7+
8+
public constructor (config, req) {
9+
super(config, req)
10+
if (GoogleCloudStorageImageCache.storage === undefined) {
11+
GoogleCloudStorageImageCache.storage = new Storage(
12+
this.moduleConfig.libraryOptions
13+
)
14+
}
15+
if (GoogleCloudStorageImageCache.bucket === undefined) {
16+
GoogleCloudStorageImageCache.bucket = GoogleCloudStorageImageCache.storage.bucket(this.bucketName)
17+
}
18+
}
19+
20+
public get bucketName (): string {
21+
return this.moduleConfig.bucket
22+
}
23+
24+
public get moduleConfig (): any {
25+
return this.config.imageable.caching[`google-cloud-storage`]
26+
}
27+
28+
public async getImageFromCache () {
29+
const donwload = await GoogleCloudStorageImageCache.bucket.file('testing/cache/image/' + this.key).download()
30+
this.image = donwload[0]
31+
}
32+
33+
public async save () {
34+
await GoogleCloudStorageImageCache.bucket.file('testing/cache/image/' + this.key).save(this.image, {
35+
gzip: true
36+
})
37+
}
38+
39+
public async check () {
40+
const response = await GoogleCloudStorageImageCache.bucket.file('testing/cache/image/' + this.key).exists()
41+
return response[0]
42+
}
43+
44+
public createKey (): string {
45+
return createHash('md5').update(this.req.url).digest('hex')
46+
}
47+
48+
public isValidFor (type) {
49+
return type === 'google-cloud-storage'
50+
}
51+
}

0 commit comments

Comments
 (0)