-
Notifications
You must be signed in to change notification settings - Fork 25
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 #3834 from thematters/feat/direct-image-upload
Direct Image Upload
- Loading branch information
Showing
17 changed files
with
367 additions
and
141 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
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,63 @@ | ||
import { FormattedMessage } from 'react-intl' | ||
|
||
import { | ||
UPLOAD_IMAGE_AREA_LIMIT, | ||
UPLOAD_IMAGE_DIMENSION_LIMIT, | ||
UPLOAD_IMAGE_SIZE_LIMIT, | ||
} from '~/common/enums' | ||
import { toast } from '~/components' | ||
|
||
export const validateImage = (image: File) => | ||
new Promise<boolean>((resolve, reject) => { | ||
// size limits | ||
const isExceedSizeLimit = image.size > UPLOAD_IMAGE_SIZE_LIMIT | ||
if (isExceedSizeLimit) { | ||
toast.error({ | ||
message: ( | ||
<FormattedMessage defaultMessage="Images have a 5 megabyte (MB) size limit." /> | ||
), | ||
}) | ||
return resolve(false) | ||
} | ||
|
||
// dimension limits | ||
try { | ||
const $img = new Image() | ||
|
||
$img.onload = () => { | ||
const { width, height } = $img | ||
window.URL.revokeObjectURL($img.src) | ||
|
||
const isExceedDimensionLimit = | ||
width > UPLOAD_IMAGE_DIMENSION_LIMIT || | ||
height > UPLOAD_IMAGE_DIMENSION_LIMIT | ||
const isExceedAreaLimit = width * height > UPLOAD_IMAGE_AREA_LIMIT | ||
|
||
if (isExceedDimensionLimit) { | ||
toast.error({ | ||
message: ( | ||
<FormattedMessage defaultMessage="Maximum image dimension is 12,000 pixels." /> | ||
), | ||
}) | ||
return resolve(false) | ||
} | ||
|
||
if (isExceedAreaLimit) { | ||
toast.error({ | ||
message: ( | ||
<FormattedMessage defaultMessage="Maximum image area is limited to 100 megapixels (for example, 10,000×10,000 pixels)." /> | ||
), | ||
}) | ||
return resolve(false) | ||
} | ||
|
||
return resolve(true) | ||
} | ||
|
||
$img.src = window.URL.createObjectURL(image) | ||
} catch (exception) { | ||
return reject(exception) | ||
} | ||
|
||
return resolve(true) | ||
}) |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './error' | ||
export * from './image' | ||
export * from './validate' |
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
Oops, something went wrong.
c81b856
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
matters-web – ./
matters-web.vercel.app
web-dev.matters.town
matters-web-git-develop-matters.vercel.app
matters-web-matters.vercel.app