Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
VictoriqueMoe committed Mar 12, 2024
1 parent 4f9f0a2 commit cfd4c71
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 53 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Victoria

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module waifuVault-go-api
module github.com/waifuvault/waifuVault-go-api

go 1.21.0
go 1.20.0
4 changes: 2 additions & 2 deletions pkg/mod/waifu_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package mod

type Waifuvalt interface {
UploadFile(options WaifuvaultPutOpts) (*WaifuResponse[string], error)
FileInfo(token string) (*WaifuResponse[string], error)
FileInfoFormatted(token string) (*WaifuResponse[int], error)
FileInfo(token string) (*WaifuResponse[int], error)
FileInfoFormatted(token string) (*WaifuResponse[string], error)
DeleteFile(token string) (bool, error)
GetFile(options GetFileInfo) ([]byte, error)
}
144 changes: 95 additions & 49 deletions pkg/main.go → pkg/waifu_vault_api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package waifuVault

import (
"bytes"
Expand All @@ -16,45 +16,11 @@ import (
const baseUrl = "https://waifuvault.moe"

var client = &http.Client{}
var WaifuVault = waifuvalt{}

func main() {
/*url := "http://localhost:3001/"
// don't worry about errors
response, _ := http.Get(url)
var target = mod.WaifuResponse[string]{}
err := json.NewDecoder(response.Body).Decode(&target)
if err != nil {
return
}
fmt.Print(target)*/
/*fileDir, _ := os.Getwd()
fileName := "main.go"
filePath := path.Join(fileDir, "pkg", fileName)
b, err := os.ReadFile(filePath)
if err != nil {
fmt.Print(err)
return
}
fileNames := "main.go"*/

opts := mod.WaifuvaultPutOpts{
Url: "https://victorique.moe/img/slider/Quotes.jpg",
HideFilename: true,
Expires: "1h",
}
file, err := WaifuVault.UploadFile(opts)
if err != nil {
fmt.Print(err)
return
}
fmt.Print(file)
type Api struct {
}

type waifuvalt struct {
}

func (re *waifuvalt) UploadFile(options mod.WaifuvaultPutOpts) (*mod.WaifuResponse[string], error) {
func (re *Api) UploadFile(options mod.WaifuvaultPutOpts) (*mod.WaifuResponse[string], error) {
if options.File != nil && options.Bytes != nil && options.Url != "" || options.File == nil && options.Bytes == nil && options.Url == "" {
return nil, errors.New("you can only supply buffer, file or url")
}
Expand Down Expand Up @@ -102,7 +68,7 @@ func (re *waifuvalt) UploadFile(options mod.WaifuvaultPutOpts) (*mod.WaifuRespon
"expires": options.Expires,
"hide_filename": options.HideFilename,
"password": options.Password,
}, nil)
}, "")

r, err := createRequest(http.MethodPut, uploadUrl, &body, writer)
if err != nil {
Expand All @@ -128,26 +94,105 @@ func createRequest(method, url string, body io.Reader, writer *multipart.Writer)
return r, nil
}

func (re *waifuvalt) FileInfo(token string) (*mod.WaifuResponse[string], error) {
return nil, nil
func (re *Api) FileInfo(token string) (*mod.WaifuResponse[int], error) {
resp, err := re.crateGetRequestForFileInfo(token, false)
if err != nil {
return nil, err
}
return getResponse[int](resp)
}

func (re *Api) FileInfoFormatted(token string) (*mod.WaifuResponse[string], error) {
resp, err := re.crateGetRequestForFileInfo(token, true)
if err != nil {
return nil, err
}
return getResponse[string](resp)
}

func (re *waifuvalt) FileInfoFormatted(token string) (*mod.WaifuResponse[int], error) {
return nil, nil
func (re *Api) crateGetRequestForFileInfo(token string, isFormatted bool) (*http.Response, error) {
getUrl := getUrl(map[string]any{"formatted": isFormatted}, token)
r, err := createRequest(http.MethodGet, getUrl, nil, nil)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
return client.Do(r)
}

func (re *waifuvalt) DeleteFile(token string) (bool, error) {
return false, nil
func (re *Api) DeleteFile(token string) (bool, error) {
deleteUrl := getUrl(nil, token)

r, err := createRequest(http.MethodDelete, deleteUrl, nil, nil)
if err != nil {
return false, err
}
resp, err := client.Do(r)
defer resp.Body.Close()

err = checkError(resp)
if err != nil {
return false, err
}
if err != nil {
return false, err
}
bodyBytes, err := io.ReadAll(resp.Body)
bodyString := string(bodyBytes)

return bodyString == "true", nil
}

func (re *waifuvalt) GetFile(options mod.GetFileInfo) ([]byte, error) {
return nil, nil
func (re *Api) GetFile(options mod.GetFileInfo) ([]byte, error) {

if options.Filename == "" && options.Token == "" {
return nil, errors.New("please supply a token or a filename")
}
var fileUrl string
if options.Filename != "" {
fileUrl = fmt.Sprintf("%s/f/%s", baseUrl, options.Filename)
} else {
fileInfo, err := re.FileInfo(options.Token)
if err != nil {
return nil, err
}
fileUrl = fileInfo.URL
}

r, err := createRequest(http.MethodGet, fileUrl, nil, nil)
if err != nil {
return nil, err
}

if options.Password != "" {
r.Header.Set("x-password", options.Password)
}

resp, err := client.Do(r)
defer resp.Body.Close()

if err != nil {
return nil, err
}

if resp.StatusCode == http.StatusForbidden {
return nil, errors.New("password is incorrect")
}

err = checkError(resp)
if err != nil {
return nil, err
}

return io.ReadAll(resp.Body)
}

func getUrl(obj map[string]any, path *string) string {
func getUrl(obj map[string]any, path string) string {
baseRestUrl := fmt.Sprintf("%s/rest", baseUrl)
if path != nil {
baseRestUrl += "/" + *path
if path != "" {
baseRestUrl = fmt.Sprintf("%s/%s", baseRestUrl, path)
}
if obj == nil {
return baseRestUrl
Expand All @@ -169,6 +214,7 @@ func getUrl(obj map[string]any, path *string) string {
}

func getResponse[T string | int](response *http.Response) (*mod.WaifuResponse[T], error) {
defer response.Body.Close()
err := checkError(response)
if err != nil {
return nil, err
Expand Down

0 comments on commit cfd4c71

Please sign in to comment.