138 lines
3.4 KiB
Go
138 lines
3.4 KiB
Go
package utils
|
|
|
|
import (
|
|
"FanslySync/structs"
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
// A base type for all message box options.
|
|
// This is a functional option pattern, so you can pass in any number of
|
|
// options to customize the message box.
|
|
type MessageBoxOption func(*runtime.MessageDialogOptions)
|
|
|
|
// WithDialogType allows you to set the dialog type for the message box.
|
|
// The default is InfoDialog.
|
|
func WithDialogType(dt runtime.DialogType) MessageBoxOption {
|
|
return func(o *runtime.MessageDialogOptions) {
|
|
o.Type = dt
|
|
}
|
|
}
|
|
|
|
// Allows you to build a custom set of buttons for the dialog.
|
|
// The default is a single “OK” button.
|
|
func WithButtons(btns []string) MessageBoxOption {
|
|
return func(o *runtime.MessageDialogOptions) {
|
|
o.Buttons = btns
|
|
}
|
|
}
|
|
|
|
// BuildMessageBoxOpts builds the options struct, defaulting to
|
|
// an Info dialog and a single “OK” button. Pass WithDialogType()
|
|
// and/or WithButtons() to customize.
|
|
func BuildMessageBoxOpts(title, message string, opts ...MessageBoxOption) runtime.MessageDialogOptions {
|
|
// defaults:
|
|
o := runtime.MessageDialogOptions{
|
|
Title: title,
|
|
Message: message,
|
|
Type: runtime.InfoDialog,
|
|
Buttons: []string{"OK"},
|
|
}
|
|
|
|
// apply any overrides:
|
|
for _, opt := range opts {
|
|
opt(&o)
|
|
}
|
|
|
|
return o
|
|
}
|
|
|
|
// ShowMessageBox shows a message box with the given options.
|
|
func ShowMessageBox(ctx context.Context, title, message string, opts ...MessageBoxOption) {
|
|
// Build the options
|
|
options := BuildMessageBoxOpts(title, message, opts...)
|
|
|
|
// Show the message box
|
|
runtime.MessageDialog(ctx, options)
|
|
}
|
|
|
|
func UploadPaste(payload []byte) (string, error) {
|
|
// Create HTTP client
|
|
client := &http.Client{}
|
|
|
|
// Max request duration is 30s
|
|
client.Timeout = 30 * time.Second
|
|
|
|
// Create request
|
|
var payloadStruct structs.PastePayload
|
|
payloadStruct.Content = string(payload)
|
|
|
|
// Marshal the payload to JSON
|
|
payloadJSON, err := json.Marshal(payloadStruct)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Create request (POST to https://paste.hep.gg/api/)
|
|
req, err := http.NewRequest("POST", "https://paste.hep.gg/api/", nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Set headers
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("User-Agent", "FanslySync/3.0 sticks@teamhydra.dev")
|
|
req.Header.Set("Accept", "application/json")
|
|
req.Header.Set("Accept-Encoding", "gzip")
|
|
|
|
// Set the body
|
|
req.Body = io.NopCloser(bytes.NewBuffer(payloadJSON))
|
|
|
|
// Send the request
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Check for non-200 status code
|
|
if resp.StatusCode != http.StatusOK {
|
|
return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
|
}
|
|
|
|
// Read the response body into our struct
|
|
var pasteResponse structs.PastePutResponse
|
|
data, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Unmarshal the response into our struct
|
|
err = json.Unmarshal(data, &pasteResponse)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Check for error in response
|
|
if pasteResponse.Error != "" {
|
|
return "", fmt.Errorf("error from paste server: %s", pasteResponse.Error)
|
|
}
|
|
|
|
// Return the paste ID
|
|
return fmt.Sprintf("https://paste.hep.gg/api/%s/raw", pasteResponse.Payload.Id), nil
|
|
}
|
|
|
|
// percentage calculates the percentage of curr out of total.
|
|
func Percentage(curr, total int) float64 {
|
|
if total == 0 {
|
|
return 0
|
|
}
|
|
return float64(curr) / float64(total) * 100
|
|
}
|