32
34
"github.com/ubuntu-core/snappy/arch"
33
35
"github.com/ubuntu-core/snappy/oauth"
36
"github.com/ubuntu-core/snappy/progress"
34
37
"github.com/ubuntu-core/snappy/release"
35
38
"github.com/ubuntu-core/snappy/snap"
36
39
"github.com/ubuntu-core/snappy/snap/remote"
188
191
return fmt.Sprintf("Snap remote repository for %s", s.searchURI)
191
// Details returns details for the given snap in this repository
192
func (s *SnapUbuntuStoreRepository) Details(name string, origin string) (parts []Part, err error) {
195
snapName = name + "." + origin
194
// Snap returns the RemoteSnapPart for the given name or an error.
195
func (s *SnapUbuntuStoreRepository) Snap(snapName string) (*RemoteSnapPart, error) {
198
197
url, err := s.detailsURI.Parse(snapName)
220
219
case resp.StatusCode == 404:
221
220
return nil, ErrPackageNotFound
222
221
case resp.StatusCode != 200:
223
return parts, fmt.Errorf("SnapUbuntuStoreRepository: unexpected http statusCode %v for %s", resp.StatusCode, snapName)
222
return nil, fmt.Errorf("SnapUbuntuStoreRepository: unexpected HTTP status code %d while looking forsnap %q", resp.StatusCode, snapName)
226
225
// and decode json
233
snap := NewRemoteSnapPart(detailsData)
234
parts = append(parts, snap)
232
return NewRemoteSnapPart(detailsData), nil
235
// Details returns details for the given snap in this repository
236
func (s *SnapUbuntuStoreRepository) Details(name string, origin string) (parts []Part, err error) {
239
snapName = name + "." + origin
241
snap, err := s.Snap(snapName)
245
return []Part{snap}, nil
239
248
// All (installable) parts from the store
383
392
func (s *SnapUbuntuStoreRepository) Installed() (parts []Part, err error) {
396
// Download downloads the given snap and returns its filename.
397
// The file is saved in temporary storage, and should be removed
398
// after use to prevent the disk from running out of space.
399
func (s *SnapUbuntuStoreRepository) Download(remoteSnap *RemoteSnapPart, pbar progress.Meter) (path string, err error) {
400
w, err := ioutil.TempFile("", remoteSnap.pkg.Name)
405
if cerr := w.Close(); cerr != nil && err == nil {
414
// try anonymous download first and fallback to authenticated
415
url := remoteSnap.pkg.AnonDownloadURL
417
url = remoteSnap.pkg.DownloadURL
419
req, err := http.NewRequest("GET", url, nil)
423
setUbuntuStoreHeaders(req)
425
if err := download(remoteSnap.Name(), w, req, pbar); err != nil {
429
return w.Name(), w.Sync()
432
// download writes an http.Request showing a progress.Meter
433
var download = func(name string, w io.Writer, req *http.Request, pbar progress.Meter) error {
434
client := &http.Client{}
436
resp, err := client.Do(req)
440
defer resp.Body.Close()
442
if resp.StatusCode != 200 {
443
return &ErrDownload{Code: resp.StatusCode, URL: req.URL}
447
pbar.Start(name, float64(resp.ContentLength))
448
mw := io.MultiWriter(w, pbar)
449
_, err = io.Copy(mw, resp.Body)
452
_, err = io.Copy(w, resp.Body)