-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from tailor-cms/chore/fix-image-storage
Fix image storage
- Loading branch information
Showing
6 changed files
with
81 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { readFile, sha256 } from './util.js'; | ||
import { storage as config } from '../../config/server/index.js'; | ||
import fromPairs from 'lodash/fromPairs.js'; | ||
import JSZip from 'jszip'; | ||
import mime from 'mime-types'; | ||
import path from 'node:path'; | ||
import pickBy from 'lodash/pickBy.js'; | ||
import request from 'axios'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
import Storage from '../../repository/storage.js'; | ||
|
||
const { getFileUrl, getPath, saveFile } = Storage; | ||
const getStorageUrl = (key) => `${config.protocol}${key}`; | ||
|
||
class StorageService { | ||
// Prefix key with custom protocol. e.g. storage://sample_key.ext | ||
getStorageUrl = (key) => `${config.protocol}${key}`; | ||
|
||
async uploadFile(repositoryId, file, name) { | ||
const buffer = await readFile(file); | ||
const hash = sha256(file.originalname, buffer); | ||
const extension = path.extname(file.originalname); | ||
const fileName = `${hash}___${name}${extension}`; | ||
const key = path.join(getPath(repositoryId), fileName); | ||
await saveFile(key, buffer, { ContentType: file.mimetype }); | ||
const publicUrl = await getFileUrl(key); | ||
return { key, publicUrl, url: getStorageUrl(key) }; | ||
} | ||
|
||
async uploadArchiveContent(repositoryId, archive, name) { | ||
const buffer = await readFile(archive); | ||
const content = await JSZip.loadAsync(buffer); | ||
const files = pickBy(content.files, (it) => !it.dir); | ||
const keys = await Promise.all( | ||
Object.keys(files).map(async (src) => { | ||
const key = path.join(getPath(repositoryId), name, src); | ||
const file = await content.file(src).async('uint8array'); | ||
const mimeType = mime.lookup(src); | ||
await saveFile(key, Buffer.from(file), { ContentType: mimeType }); | ||
return [key, getStorageUrl(key)]; | ||
}), | ||
); | ||
return fromPairs(keys); | ||
} | ||
|
||
async downloadToStorage(url) { | ||
const res = await request.get(url, { responseType: 'arraybuffer' }); | ||
const filename = path.join( | ||
getPath(), | ||
`${uuidv4()}__${url.pathname.split('/').pop()}`, | ||
); | ||
await Storage.saveFile(filename, res.data); | ||
return getStorageUrl(filename); | ||
} | ||
} | ||
|
||
export default new StorageService(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.