@667/ghost-storage-github
Advanced tools
Comparing version 1.0.0 to 1.1.0
47
index.js
@@ -6,2 +6,3 @@ const https = require('https') | ||
const BaseAdapter = require('./BaseStorage') | ||
const sharp = require('sharp') | ||
@@ -14,3 +15,5 @@ const { | ||
GHOST_STORAGE_GITHUB_DESTINATION, | ||
GHOST_STORAGE_GITHUB_ORIGIN | ||
GHOST_STORAGE_GITHUB_ORIGIN, | ||
GHOST_STORAGE_GITHUB_IMAGE_FORMAT, | ||
GHOST_STORAGE_GITHUB_ETAG_CACHE_SIZE | ||
} = process.env | ||
@@ -29,2 +32,4 @@ | ||
origin, | ||
imageFormat, | ||
etagCacheSize | ||
} = config | ||
@@ -38,2 +43,4 @@ | ||
this.origin = GHOST_STORAGE_GITHUB_ORIGIN || origin | ||
this.imageFormat = GHOST_STORAGE_GITHUB_IMAGE_FORMAT || imageFormat | ||
this.etagCacheSize = GHOST_STORAGE_GITHUB_ETAG_CACHE_SIZE || etagCacheSize || 10000 | ||
this.committer = { | ||
@@ -88,2 +95,29 @@ name: 'June07', | ||
} | ||
async getBase64(buffer) { | ||
if (!this.imageFormat) { | ||
return buffer.toString('base64') | ||
} | ||
const { fileTypeFromBuffer } = await import('file-type') | ||
const { mime } = await fileTypeFromBuffer(buffer) | ||
if (/image/.test(mime) && mime.match(/image\/(.*)/)[1] !== this.imageFormat) { | ||
return await sharp(buffer).toFormat(this.imageFormat).toBuffer() | ||
} | ||
return buffer.toString('base64') | ||
} | ||
checkSizeAndEvict() { | ||
const keys = Object.keys(this.etags) | ||
if (keys.length > this.etagCacheSize) { | ||
// Sort keys by timestamp | ||
keys.sort((a, b) => this.etags[a].timestamp - this.etags[b].timestamp) | ||
// Remove the oldest entries until the size is within the limit | ||
while (keys.length > this.etagCacheSize) { | ||
const oldestKey = keys.shift() | ||
delete this.etags[oldestKey] | ||
} | ||
} | ||
} | ||
async exists(file, targetDir) { | ||
@@ -103,3 +137,7 @@ const sha = createHash('sha1').update(file.base64).digest('hex') | ||
const shaResponse = createHash('sha1').update(base64Response).digest('hex') | ||
this.etags[sha] = headers.etag | ||
this.etags[sha] = { | ||
timestamp: Date.now(), | ||
etag: headers.etag | ||
} | ||
this.checkSizeAndEvict() | ||
@@ -118,3 +156,5 @@ if (shaResponse === sha) { | ||
async save(file, targetDir) { | ||
const base64 = await readFile(file.path, 'base64') | ||
const buffer = await readFile(file.path) | ||
const base64 = await this.getBase64(buffer) | ||
const filepath = await this.getUniqueFileName({ ...file, base64 }, targetDir || this.getTargetDir()) | ||
@@ -141,3 +181,2 @@ const filename = filepath.split('/').pop() | ||
} | ||
serve() { | ||
@@ -144,0 +183,0 @@ return (req, res, next) => next |
{ | ||
"name": "@667/ghost-storage-github", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Save Ghost Blog Content to Github", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "echo \"Error: no test specified\" && exit 0" | ||
}, | ||
@@ -27,4 +27,6 @@ "repository": { | ||
"@octokit/plugin-throttling": "^9.3.0", | ||
"@octokit/rest": "^21.0.0" | ||
"@octokit/rest": "^21.0.0", | ||
"file-type": "^19.0.0", | ||
"sharp": "^0.33.4" | ||
} | ||
} |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
10413
4
224
5
+ Addedfile-type@^19.0.0
+ Addedsharp@^0.33.4
+ Added@emnapi/runtime@1.3.1(transitive)
+ Added@img/sharp-darwin-arm64@0.33.5(transitive)
+ Added@img/sharp-darwin-x64@0.33.5(transitive)
+ Added@img/sharp-libvips-darwin-arm64@1.0.4(transitive)
+ Added@img/sharp-libvips-darwin-x64@1.0.4(transitive)
+ Added@img/sharp-libvips-linux-arm@1.0.5(transitive)
+ Added@img/sharp-libvips-linux-arm64@1.0.4(transitive)
+ Added@img/sharp-libvips-linux-s390x@1.0.4(transitive)
+ Added@img/sharp-libvips-linux-x64@1.0.4(transitive)
+ Added@img/sharp-libvips-linuxmusl-arm64@1.0.4(transitive)
+ Added@img/sharp-libvips-linuxmusl-x64@1.0.4(transitive)
+ Added@img/sharp-linux-arm@0.33.5(transitive)
+ Added@img/sharp-linux-arm64@0.33.5(transitive)
+ Added@img/sharp-linux-s390x@0.33.5(transitive)
+ Added@img/sharp-linux-x64@0.33.5(transitive)
+ Added@img/sharp-linuxmusl-arm64@0.33.5(transitive)
+ Added@img/sharp-linuxmusl-x64@0.33.5(transitive)
+ Added@img/sharp-wasm32@0.33.5(transitive)
+ Added@img/sharp-win32-ia32@0.33.5(transitive)
+ Added@img/sharp-win32-x64@0.33.5(transitive)
+ Added@sec-ant/readable-stream@0.4.1(transitive)
+ Added@tokenizer/token@0.3.0(transitive)
+ Addedcolor@4.2.3(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedcolor-string@1.9.1(transitive)
+ Addeddetect-libc@2.0.3(transitive)
+ Addedfile-type@19.6.0(transitive)
+ Addedget-stream@9.0.1(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedis-arrayish@0.3.2(transitive)
+ Addedis-stream@4.0.1(transitive)
+ Addedpeek-readable@5.4.2(transitive)
+ Addedsemver@7.7.1(transitive)
+ Addedsharp@0.33.5(transitive)
+ Addedsimple-swizzle@0.2.2(transitive)
+ Addedstrtok3@9.1.1(transitive)
+ Addedtoken-types@6.0.0(transitive)
+ Addedtslib@2.8.1(transitive)
+ Addeduint8array-extras@1.4.0(transitive)