-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
64 lines (50 loc) · 1.33 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package msgraph
import (
"context"
"fmt"
"io/ioutil"
"log"
"mime"
"net/http"
"github.com/joefitzgerald/gabby"
)
func GetFileContentType(b []byte) string {
buffer := b[:512]
contentType := http.DetectContentType(buffer)
return contentType
}
func (a *API) GetName(ctx context.Context, id string) (string, error) {
u, err := a.Client.Users().ID(id).Request().Get(ctx)
if err != nil {
log.Println("unable to get user details")
return "", err
}
return *u.DisplayName, nil
}
func (a *API) GetPhoto(ctx context.Context, id string) (*gabby.Photo, error) {
pr, err := a.Client.Users().ID(id).Request().NewRequest("GET", "/photo/$value", nil)
if err != nil {
log.Println("Unable to create photo request")
return nil, err
}
r, err := a.HttpClient.Do(pr)
if r.StatusCode != http.StatusOK {
return nil, fmt.Errorf("no photo found for ID: '%s' (%d)", id, r.StatusCode)
}
if err != nil {
log.Println("Unable to retrieve photo")
return nil, err
}
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("Unable to read photo binary data")
return nil, err
}
exts, err := mime.ExtensionsByType(GetFileContentType(b))
if err != nil {
log.Println("Unable to determine extension from content type")
return nil, err
}
return &gabby.Photo{Data: b, ID: id, SuggestedExtension: exts[0]}, nil
}