mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
# Conflicts: # core/record_field_resolver_multi_match.go # ui/dist/assets/AuthMethodsDocs-ChShXLwE.js # ui/dist/assets/AuthMethodsDocs-Cqvl0T_l.js # ui/dist/assets/AuthMethodsDocs-DIqhS7qY.js # ui/dist/assets/AuthRefreshDocs-CHakkNnn.js # ui/dist/assets/AuthRefreshDocs-DLb1qree.js # ui/dist/assets/AuthRefreshDocs-DXgrlMdk.js # ui/dist/assets/AuthWithOAuth2Docs-Bl8VRxNv.js # ui/dist/assets/AuthWithOAuth2Docs-DVBOel22.js # ui/dist/assets/AuthWithOAuth2Docs-I213UiJ7.js # ui/dist/assets/AuthWithOtpDocs-B9tL6gIN.js # ui/dist/assets/AuthWithOtpDocs-C2PPm9-Z.js # ui/dist/assets/AuthWithOtpDocs-qEMoMG3D.js # ui/dist/assets/AuthWithPasswordDocs-B8JWDbPL.js # ui/dist/assets/AuthWithPasswordDocs-BTHeYJwE.js # ui/dist/assets/AuthWithPasswordDocs-D9rqgLpO.js # ui/dist/assets/BatchApiDocs-BV4PYYXn.js # ui/dist/assets/BatchApiDocs-DXa997am.js # ui/dist/assets/BatchApiDocs-Di2Qe2dV.js # ui/dist/assets/DeleteApiDocs-C3IINRwz.js # ui/dist/assets/DeleteApiDocs-LfmVp4Uf.js # ui/dist/assets/DeleteApiDocs-SU4XdGhJ.js # ui/dist/assets/EmailChangeDocs-COx1USU5.js # ui/dist/assets/EmailChangeDocs-D-qNHT_P.js # ui/dist/assets/EmailChangeDocs-DpfXIE_V.js # ui/dist/assets/FieldsQueryParam-BYe9ZK-G.js # ui/dist/assets/FieldsQueryParam-L8vpHeF4.js # ui/dist/assets/FieldsQueryParam-z0dGQ6uY.js # ui/dist/assets/ListApiDocs-BCTT-AOz.js # ui/dist/assets/ListApiDocs-C0_GwI7k.js # ui/dist/assets/ListApiDocs-T9_aO-pW.js # ui/dist/assets/PageInstaller-B22bivaN.js # ui/dist/assets/PageInstaller-C0BBhyBx.js # ui/dist/assets/PageInstaller-IYSwJhZW.js # ui/dist/assets/PageOAuth2RedirectFailure-BjbpqlFj.js # ui/dist/assets/PageOAuth2RedirectFailure-COyNF79R.js # ui/dist/assets/PageOAuth2RedirectFailure-DPmGG9zv.js # ui/dist/assets/PageOAuth2RedirectSuccess-6cMXLFAX.js # ui/dist/assets/PageOAuth2RedirectSuccess-CdkCarGH.js # ui/dist/assets/PageOAuth2RedirectSuccess-DSEIHeQL.js # ui/dist/assets/PageRecordConfirmEmailChange-BYW-6ASg.js # ui/dist/assets/PageRecordConfirmEmailChange-Bm1CIYvq.js # ui/dist/assets/PageRecordConfirmEmailChange-BpaqOT4u.js # ui/dist/assets/PageRecordConfirmPasswordReset--wx_u2lI.js # ui/dist/assets/PageRecordConfirmPasswordReset-CoeY2Z_C.js # ui/dist/assets/PageRecordConfirmPasswordReset-Ptya5bfe.js # ui/dist/assets/PageRecordConfirmVerification-6xjF3T0O.js # ui/dist/assets/PageRecordConfirmVerification-C1UTIIJh.js # ui/dist/assets/PageRecordConfirmVerification-C4LixXvA.js # ui/dist/assets/PageSuperuserConfirmPasswordReset-19z2IFg4.js # ui/dist/assets/PageSuperuserConfirmPasswordReset-DwNi0ugQ.js # ui/dist/assets/PageSuperuserConfirmPasswordReset-SMnDTRNi.js # ui/dist/assets/PageSuperuserRequestPasswordReset-BpDW217f.js # ui/dist/assets/PageSuperuserRequestPasswordReset-DK63qE6z.js # ui/dist/assets/PageSuperuserRequestPasswordReset-bG02qLIM.js # ui/dist/assets/PasswordResetDocs-CE67mLC0.js # ui/dist/assets/PasswordResetDocs-CHAK0vma.js # ui/dist/assets/PasswordResetDocs-Dap-N4rx.js # ui/dist/assets/RealtimeApiDocs-BooTSQCb.js # ui/dist/assets/RealtimeApiDocs-CoHmYVEx.js # ui/dist/assets/RealtimeApiDocs-D7ds3fHt.js # ui/dist/assets/VerificationDocs-B42AIoCQ.js # ui/dist/assets/VerificationDocs-BS7b11zH.js # ui/dist/assets/VerificationDocs-ZhkdFVIl.js # ui/dist/assets/ViewApiDocs-BXmRLHZ1.js # ui/dist/assets/ViewApiDocs-DM5JJQjh.js # ui/dist/assets/ViewApiDocs-DRpxcuJR.js # ui/public/fonts/source-sans-pro/source-sans-pro-v18-latin_cyrillic-700.woff2 # ui/public/fonts/ubuntu-mono/ubuntu-mono-v17-cyrillic_latin-700.woff2
150 lines
3.3 KiB
Go
150 lines
3.3 KiB
Go
package jsvm
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"mime/multipart"
|
|
|
|
"github.com/hanzoai/base/tools/filesystem"
|
|
"github.com/spf13/cast"
|
|
)
|
|
|
|
// FormData represents an interface similar to the browser's [FormData].
|
|
//
|
|
// The value of each FormData entry must be a string or [*filesystem.File] instance.
|
|
//
|
|
// It is intended to be used together with the JSVM `$http.send` when
|
|
// sending multipart/form-data requests.
|
|
//
|
|
// [FormData]: https://developer.mozilla.org/en-US/docs/Web/API/FormData.
|
|
type FormData map[string][]any
|
|
|
|
// Append appends a new value onto an existing key inside the current FormData,
|
|
// or adds the key if it does not already exist.
|
|
func (data FormData) Append(key string, value any) {
|
|
data[key] = append(data[key], value)
|
|
}
|
|
|
|
// Set sets a new value for an existing key inside the current FormData,
|
|
// or adds the key/value if it does not already exist.
|
|
func (data FormData) Set(key string, value any) {
|
|
data[key] = []any{value}
|
|
}
|
|
|
|
// Delete deletes a key and its value(s) from the current FormData.
|
|
func (data FormData) Delete(key string) {
|
|
delete(data, key)
|
|
}
|
|
|
|
// Get returns the first value associated with a given key from
|
|
// within the current FormData.
|
|
//
|
|
// If you expect multiple values and want all of them,
|
|
// use the [FormData.GetAll] method instead.
|
|
func (data FormData) Get(key string) any {
|
|
values, ok := data[key]
|
|
if !ok || len(values) == 0 {
|
|
return nil
|
|
}
|
|
|
|
return values[0]
|
|
}
|
|
|
|
// GetAll returns all the values associated with a given key
|
|
// from within the current FormData.
|
|
func (data FormData) GetAll(key string) []any {
|
|
values, ok := data[key]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
return values
|
|
}
|
|
|
|
// Has returns whether a FormData object contains a certain key.
|
|
func (data FormData) Has(key string) bool {
|
|
values, ok := data[key]
|
|
|
|
return ok && len(values) > 0
|
|
}
|
|
|
|
// Keys returns all keys contained in the current FormData.
|
|
func (data FormData) Keys() []string {
|
|
result := make([]string, 0, len(data))
|
|
|
|
for k := range data {
|
|
result = append(result, k)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// Keys returns all values contained in the current FormData.
|
|
func (data FormData) Values() []any {
|
|
result := make([]any, 0, len(data))
|
|
|
|
for _, values := range data {
|
|
result = append(result, values...)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// Entries returns a [key, value] slice pair for each FormData entry.
|
|
func (data FormData) Entries() [][]any {
|
|
result := make([][]any, 0, len(data))
|
|
|
|
for k, values := range data {
|
|
for _, v := range values {
|
|
result = append(result, []any{k, v})
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// toMultipart converts the current FormData entries into multipart encoded data.
|
|
func (data FormData) toMultipart() (*bytes.Buffer, *multipart.Writer, error) {
|
|
body := new(bytes.Buffer)
|
|
|
|
mp := multipart.NewWriter(body)
|
|
defer mp.Close()
|
|
|
|
for k, values := range data {
|
|
for _, rawValue := range values {
|
|
switch v := rawValue.(type) {
|
|
case *filesystem.File:
|
|
err := func() error {
|
|
mpw, err := mp.CreateFormFile(k, v.OriginalName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
file, err := v.Reader.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
|
|
_, err = io.Copy(mpw, file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
default:
|
|
err := mp.WriteField(k, cast.ToString(v))
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return body, mp, nil
|
|
}
|