Skip to content

Commit

Permalink
implement maxSize in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
ybizeul committed Nov 8, 2024
1 parent fd3a9cb commit 2059e44
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 60 deletions.
13 changes: 13 additions & 0 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func (api *ApiHandler) GetServer() *chi.Mux {
})

r.Post("/api/secrets", api.postSecretsHandler)
r.Get("/api/infos", api.getInfosHandler)
r.Route("/api/feeds", func(r chi.Router) {
r.Get("/{feedName}", api.feedGetFunc)
r.Post("/{feedName}", api.feedPostFunc)
Expand All @@ -211,6 +212,18 @@ func (api *ApiHandler) GetServer() *chi.Mux {
return r
}

func (api *ApiHandler) getInfosHandler(w http.ResponseWriter, r *http.Request) {
result := struct {
Version string `json:"version"`
MaxBodySize int `json:"maxBodySize"`
}{
Version: api.Version,
MaxBodySize: api.MaxBodySize,
}

_ = json.NewEncoder(w).Encode(result)
}

func (api *ApiHandler) feedWSHandler(w http.ResponseWriter, r *http.Request) {

secret, _ := utils.GetSecret(r)
Expand Down
74 changes: 14 additions & 60 deletions web/ui/src/YBFeed/Components/YBPasteCardComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,74 +10,28 @@ import './YBPasteCardComponent.css'
import { PasteToFeed } from '../../paste';
import { Y } from '../../YBFeedClient';

interface ServerInfo {
maxBodySize: number
}
export function YBPasteCardComponent() {
const [isMobile, setIsMobile] = useState(false)
const [maxSize, setMaxSize] = useState(5*1024**2)

const {feedName} = useParams()

if (!feedName) {
redirect("/")
return
}
// const form = useForm({
// initialValues: {
// text: '',
// },
// })

// //
// // Pasting Data
// //

// const handleOnPaste = (event: React.ClipboardEvent) => {
// const items = event.clipboardData.items
// let data, type
// form.setFieldValue("text","")
// for (let i=0; i<items.length;i++) {
// if (items[i].type.indexOf("image") === 0 && items[i].kind === "file") {
// type = items[i].type
// data = items[i].getAsFile()
// break
// }
// else if (items[i].type === "text/plain") {
// type = items[i].type
// data = event.clipboardData.getData('text')
// break
// }
// }

// if (type === undefined) {
// return
// }

// const requestHeaders: HeadersInit = new Headers();
// requestHeaders.set("Content-Type", type)
// fetch("/api/feeds/" + encodeURIComponent(feed!),{
// method: "POST",
// body: data,
// headers: requestHeaders,
// credentials: "include"
// })
// .then(() => {
// form.setFieldValue("text","")
// if (props.onPaste) {
// props.onPaste()
// }
// })
// }

// const handleFinish = (text:string) => {
// const requestHeaders: HeadersInit = new Headers();
// requestHeaders.set("Content-Type", "text/plain")
// fetch("/api/feeds/" + encodeURIComponent(feed!),{
// method: "POST",
// body: text,
// headers: requestHeaders,
// credentials: "include"
// })
// .then(() => {
// form.setFieldValue("text","")
// })
// }
useEffect(() => {
Y.get("/infos").then(r => {
const i = r as ServerInfo
setMaxSize(i.maxBodySize)
}).catch(e => {
console.log(e)
})
})

useEffect(() => {
const handleResize = () => {
Expand Down Expand Up @@ -117,7 +71,7 @@ export function YBPasteCardComponent() {
Y.post("/feeds/" + encodeURIComponent(feedName), formData)
}}
onReject={(files) => console.log('rejected files', files)}
maxSize={5 * 1024 ** 2}
maxSize={maxSize}
><Center h={"100vh"}>
Drop files here
</Center>
Expand Down

0 comments on commit 2059e44

Please sign in to comment.