add front-end size checking

This commit is contained in:
Peter Cai 2020-02-20 13:17:55 +08:00
parent 6c15b51296
commit 3a3ea823a9
No known key found for this signature in database
GPG Key ID: 71F5FB4E4F3FD54F
3 changed files with 22 additions and 1 deletions

View File

@ -34,6 +34,7 @@ export default BinaryUpload = ->
doPaste name, mime, encrypted, (url) ->
url + "?crypt#" + key + "+" + iv
doUpload = useCallback doUpload, [file, encrypt, doPaste]
doUpload = hooks.useCheckSize file?.size, doUpload
<div className="content-pastebin">
<section className="container">

View File

@ -1,6 +1,7 @@
import React, { useState, useCallback, useEffect, useContext } from "react"
import ReactModal from "react-modal"
import DialogContext from "./dialogContext"
import * as util from "../util"
# Simple abstraction for a toggling state
export useToggle = (defVal) ->
@ -151,4 +152,22 @@ export useFetchContent = (id, callback) ->
return newMeta
# Use async memo
useAsyncMemo null, doFetch, []
useAsyncMemo null, doFetch, []
# Convenient short-hand for checking content length before upload
# Wrap this around some function to get the length-checking behavior for free
# Returned function only calls callback if the content length is within limits
# Otherwise a dialog will be opened
export useCheckSize = (size, callback) ->
openDialog = useContext DialogContext
doCheck = ->
if size >= util.MAX_UPLOAD_SIZE
openDialog do ->
"Maximum Upload Size: #{util.humanFileSize util.MAX_UPLOAD_SIZE}"
else if size == 0
openDialog "Empty content is not allowed."
else
callback arguments
useCallback doCheck, [size, callback, openDialog]

View File

@ -24,6 +24,7 @@ export default Pastebin = ->
# Paste depends only on the actual text
# and of course the function doPaste
paste = useCallback paste, [text, doPaste]
paste = hooks.useCheckSize text.length, paste
<div className="content-pastebin">
<ContentEditable