Skip to content

Commit

Permalink
Merge pull request #10 from waifuvault/update-api-to-add-modify-api
Browse files Browse the repository at this point in the history
add modify
  • Loading branch information
VictoriqueMoe authored Mar 20, 2024
2 parents 5e56036 + d620344 commit cfd3bdb
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 1 deletion.
19 changes: 19 additions & 0 deletions pkg/mod/modify_entry_payload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mod

// ModifyEntryPayload A modify entry request to change aspects of the entry
type ModifyEntryPayload struct {

// The new password.
// If the file is not currently encrypted, then this will encrypt it with the new password if it is encrypted, Then this will change the password (`previousPassword` will need to be set in this case)
// set this to an empty string `""` to remove protection and decrypt the file
Password *string `json:"password"`

// If changing a password, then this will need to be set
PreviousPassword *string `json:"previousPassword"`

// same as WaifuvaultPutOpts.Expires
CustomExpiry *string `json:"customExpiry"`

// hide the filename. use the new URL in the response to get the new URL to use
HideFilename *bool `json:"hideFilename"`
}
3 changes: 3 additions & 0 deletions pkg/mod/waifu_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ type Waifuvalt interface {

// GetFile - Download the file given options and return a byte array of said file
GetFile(ctx context.Context, options GetFileInfo) ([]byte, error)

// ModifyFile - modify an entry
ModifyFile(ctx context.Context, token string, options ModifyEntryPayload) (*WaifuResponse[int], error)
}
18 changes: 18 additions & 0 deletions pkg/waifu_vault_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ func (re *api) GetFile(ctx context.Context, options mod.GetFileInfo) ([]byte, er
return io.ReadAll(resp.Body)
}

func (re *api) ModifyFile(ctx context.Context, token string, options mod.ModifyEntryPayload) (*mod.WaifuResponse[int], error) {
uploadUrl := getUrl(nil, token)

jsonData, err := json.Marshal(options)
if err != nil {
return nil, err
}
r, err := re.createRequest(ctx, http.MethodPatch, uploadUrl, bytes.NewBuffer(jsonData), nil)
if err != nil {
return nil, err
}
resp, err := re.client.Do(r)
if err != nil {
return nil, err
}
return getResponse[int](resp)
}

func getUrl(obj map[string]any, path string) string {
baseRestUrl := fmt.Sprintf("%s/rest", baseUrl)
if path != "" {
Expand Down
148 changes: 147 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This API contains 4 interactions:
2. [Get File Info](#get-file-info)
3. [Delete File](#delete-file)
4. [Get File](#get-file)
5. [Modify Entry](#modify-entry)

The package is namespaced to `waifuVault`, so to import it, simply:

Expand Down Expand Up @@ -46,7 +47,6 @@ func main() {
}
```


### Upload File<a id="upload-file"></a>

To Upload a file, use the `UploadFile` function. This function takes the following options as struct:
Expand Down Expand Up @@ -323,3 +323,149 @@ func main() {
fmt.Print(bytes) // byte array
}
```

### Modify Entry<a id="modify-entry"></a>

If you want to modify aspects of your entry such as password, removing password, decrypting the file, encrypting the
file, changing the expiry, etc. you can use `ModifyFile` function

Use the `ModifyFile` function. This function takes the following options an object and one as a parameter:

| parameter | Type | Description | Required |
|-----------|----------|------------------------------------------|----------|
| `token` | `string` | The token of the file you want to modify | true |

Options:

| Option | Type | Description | Required | Extra info |
|--------------------|-----------|----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------|----------------------------------------------------------------------------------------|
| `Password` | `*string` | The new password or the password you want to use to encrypt the file | false | |
| `PreviousPassword` | `*string` | If the file is currently protected or encrpyted and you want to change it, use this for the old password | true only if `password` is set and the file is currently protected | if the file is protected already and you want to change the password, this MUST be set |
| `CustomExpiry` | `*string` | a new custom expiry, see `Expires` in `UploadFile` | false | |
| `HideFilename` | `*bool` | make the filename hidden | false | |

to use this, it is needed that you use a toPtr function as this struct contains pointers:

```go
package main

func ToPtr[T any](x T) *T {
return &x
}
```

Set a password on a non-encrypted file:

```go
package main

import (
"context"
"fmt"
waifuVault "github.com/waifuvault/waifuVault-go-api/pkg"
"github.com/waifuvault/waifuVault-go-api/pkg/mod"
"net/http"
)

func main() {
api := waifuVault.NewWaifuvaltApi(http.Client{})
aa, err := api.ModifyFile(context.TODO(), "eb1fe7c9-4e55-4d73-bcb9-6d1906ec9e2c", mod.ModifyEntryPayload{
Password: ToPtr("foo"),
})

if err != nil {
fmt.Print(err)
}
fmt.Print(aa.Protected) // true
}
func ToPtr[T any](x T) *T {
return &x
}
```

Change a password:

```go
package main

import (
"context"
"fmt"
waifuVault "github.com/waifuvault/waifuVault-go-api/pkg"
"github.com/waifuvault/waifuVault-go-api/pkg/mod"
"net/http"
)

func main() {
api := waifuVault.NewWaifuvaltApi(http.Client{})
aa, err := api.ModifyFile(context.TODO(), "eb1fe7c9-4e55-4d73-bcb9-6d1906ec9e2c", mod.ModifyEntryPayload{
Password: ToPtr("updated"),
PreviousPassword: ToPtr("foo"),
})

if err != nil {
fmt.Print(err)
}
fmt.Print(aa.Protected) // true
}
func ToPtr[T any](x T) *T {
return &x
}
```

change expire:

```go
package main

import (
"context"
"fmt"
waifuVault "github.com/waifuvault/waifuVault-go-api/pkg"
"github.com/waifuvault/waifuVault-go-api/pkg/mod"
"net/http"
)

func main() {
api := waifuVault.NewWaifuvaltApi(http.Client{})
_, err := api.ModifyFile(context.TODO(), "eb1fe7c9-4e55-4d73-bcb9-6d1906ec9e2c", mod.ModifyEntryPayload{
CustomExpiry: ToPtr("1d"),
})
if err != nil {
fmt.Print(err)
}
}
func ToPtr[T any](x T) *T {
return &x
}
```

decrypt a file and remove the password:

```go
package main

import (
"context"
"fmt"
waifuVault "github.com/waifuvault/waifuVault-go-api/pkg"
"github.com/waifuvault/waifuVault-go-api/pkg/mod"
"net/http"
)

func main() {
api := waifuVault.NewWaifuvaltApi(http.Client{})
aa, err := api.ModifyFile(context.TODO(), "eb1fe7c9-4e55-4d73-bcb9-6d1906ec9e2c", mod.ModifyEntryPayload{
Password: ToPtr(""),
PreviousPassword: ToPtr("foo"),
})
if err != nil {
fmt.Print(err)
}
fmt.Print(aa.Protected) // false
}

func ToPtr[T any](x T) *T {
return &x
}
```

0 comments on commit cfd3bdb

Please sign in to comment.