Skip to content

Commit

Permalink
Modified msauth/storage.go
Browse files Browse the repository at this point in the history
Both `ReadLocation()` and `WriteLocation()` were simplified to act as a proxy for calling `ioutil.ReadFile` and `ioutil.WriteFile`. The original implementation was failing on Windows when using an absolute path, and seeing that the library hasn't been updated in two years, I've decided to fork it for myself.
  • Loading branch information
theriverman committed Nov 8, 2022
1 parent a4e9fb2 commit eace58c
Showing 1 changed file with 4 additions and 53 deletions.
57 changes: 4 additions & 53 deletions msauth/storage.go
Expand Up @@ -10,61 +10,12 @@ import (
"strings"
)

// ReadLocation reads data from file with path or URL
// ReadLocation reads data from file
func ReadLocation(loc string) ([]byte, error) {
u, err := url.Parse(loc)
if err != nil {
return nil, err
}
switch u.Scheme {
case "", "file":
return ioutil.ReadFile(u.Path)
case "http", "https":
res, err := http.Get(loc)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s", res.Status)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return b, nil
}
return nil, fmt.Errorf("Unsupported location to load: %s", loc)
return ioutil.ReadFile(loc)
}

// WriteLocation writes data to file with path or URL
// WriteLocation writes data to file
func WriteLocation(loc string, b []byte, m os.FileMode) error {
u, err := url.Parse(loc)
if err != nil {
return err
}
switch u.Scheme {
case "", "file":
return ioutil.WriteFile(u.Path, b, m)
case "http", "https":
if strings.HasSuffix(u.Host, ".blob.core.windows.net") {
// Azure Blob Storage URL with SAS assumed here
cli := &http.Client{}
req, err := http.NewRequest(http.MethodPut, loc, bytes.NewBuffer(b))
if err != nil {
return err
}
req.Header.Set("x-ms-blob-type", "BlockBlob")
res, err := cli.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusCreated {
return fmt.Errorf("%s", res.Status)
}
return nil
}
}
return fmt.Errorf("Unsupported location to save: %s", loc)
return ioutil.WriteFile(loc, b, m)
}

0 comments on commit eace58c

Please sign in to comment.