-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9ea889
commit fba3532
Showing
10 changed files
with
645 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.6.0 | ||
0.7.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package dvls | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
type EntryAttachment struct { | ||
ID string `json:"id,omitempty"` | ||
IDString string `json:"idString"` | ||
EntryID string `json:"connectionID"` | ||
EntryIDString string `json:"connectionIDString"` | ||
Description string `json:"description"` | ||
FileName string `json:"filename"` | ||
IsPrivate bool `json:"isPrivate"` | ||
Size int `json:"size"` | ||
Title string `json:"title"` | ||
} | ||
|
||
// UnmarshalJSON implements the json.Unmarshaler interface. | ||
func (e *EntryAttachment) UnmarshalJSON(d []byte) error { | ||
type rawEntryAttachment EntryAttachment | ||
raw := struct { | ||
Data rawEntryAttachment `json:"data"` | ||
}{} | ||
|
||
err := json.Unmarshal(d, &raw) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*e = EntryAttachment(raw.Data) | ||
|
||
return nil | ||
} | ||
|
||
const attachmentEndpoint = "/api/attachment" | ||
|
||
func (c *Client) newAttachmentRequest(attachment EntryAttachment) (string, error) { | ||
reqUrl, err := url.JoinPath(c.baseUri, attachmentEndpoint, "save?=&private=false&useSensitiveMode=true") | ||
if err != nil { | ||
return "", fmt.Errorf("failed to build attachment url. error: %w", err) | ||
} | ||
|
||
reqUrl, err = url.QueryUnescape(reqUrl) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to unescape query url. error: %w", err) | ||
} | ||
|
||
entryJson, err := json.Marshal(attachment) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to marshal body. error: %w", err) | ||
} | ||
|
||
resp, err := c.Request(reqUrl, http.MethodPost, bytes.NewBuffer(entryJson)) | ||
if err != nil { | ||
return "", fmt.Errorf("error while submitting entry attachment request. error: %w", err) | ||
} else if err = resp.CheckRespSaveResult(); err != nil { | ||
return "", err | ||
} | ||
|
||
err = json.Unmarshal(resp.Response, &attachment) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to unmarshal response body. error: %w", err) | ||
} | ||
|
||
return attachment.ID, nil | ||
} | ||
|
||
func (c *Client) uploadAttachment(fileBytes []byte, attachmentId string) error { | ||
reqUrl, err := url.JoinPath(c.baseUri, attachmentEndpoint, attachmentId, "document") | ||
if err != nil { | ||
return fmt.Errorf("failed to build attachment url. error: %w", err) | ||
} | ||
|
||
contentType := http.DetectContentType(fileBytes) | ||
|
||
resp, err := c.Request(reqUrl, http.MethodPost, bytes.NewBuffer(fileBytes), RequestOptions{ContentType: contentType}) | ||
if err != nil { | ||
return fmt.Errorf("error while uploading entry attachment. error: %w", err) | ||
} else if err = resp.CheckRespSaveResult(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.