This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 #2928 from johnnynotsolucky/local-image-storage
Add local file storage for dev environments
- Loading branch information
Showing
12 changed files
with
79 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,4 @@ api/.env | |
.expo | ||
mobile/.expo | ||
test-results.json | ||
public/uploads |
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
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,27 @@ | ||
// @flow | ||
require('now-env'); | ||
|
||
import type { FileUpload, EntityTypes } from 'shared/types'; | ||
|
||
const { FILE_STORAGE } = process.env; | ||
|
||
const getUploadImageFn = () => { | ||
switch (FILE_STORAGE) { | ||
case 'local': | ||
return require('./file-system').uploadImage; | ||
case 's3': | ||
default: | ||
return require('./s3').uploadImage; | ||
} | ||
}; | ||
|
||
const uploadImageFn = getUploadImageFn(); | ||
|
||
export const uploadImage = ( | ||
file: FileUpload, | ||
entity: EntityTypes, | ||
id: string | ||
): Promise<string> => | ||
uploadImageFn(file, entity, id).catch(err => { | ||
throw new Error(err); | ||
}); |
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,40 @@ | ||
// @flow | ||
import shortid from 'shortid'; | ||
import fs from 'fs'; | ||
|
||
import type { FileUpload, EntityTypes } from 'shared/types'; | ||
|
||
const STORAGE_DIR = 'public/uploads'; | ||
const READ_WRITE_MODE = 0o777; | ||
|
||
const dirExists = (path: string): Promise<boolean> => | ||
new Promise(res => fs.access(path, fs.constants.F_OK, err => res(!!!err))); | ||
|
||
const createUploadsDir = (path: string): Promise<void> => | ||
new Promise(res => | ||
fs.mkdir(path, READ_WRITE_MODE, err => { | ||
if (err) throw new Error(err); | ||
res(); | ||
}) | ||
); | ||
|
||
export const uploadImage = async ( | ||
file: FileUpload, | ||
entity: EntityTypes, | ||
id: string | ||
): Promise<string> => { | ||
const result = await file; | ||
|
||
if (!await dirExists(STORAGE_DIR)) { | ||
await createUploadsDir(STORAGE_DIR); | ||
} | ||
|
||
return new Promise(res => { | ||
const filePath = `${shortid.generate()}-${entity}-${id}`; | ||
const { stream } = result; | ||
stream.pipe(fs.createWriteStream(`${STORAGE_DIR}/${filePath}`)); | ||
stream.on('end', () => { | ||
res(encodeURI(`/uploads/${filePath}`)); | ||
}); | ||
}); | ||
}; |
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