github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/daemon/api_icons.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2015-2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package daemon
    21  
    22  import (
    23  	"net/http"
    24  
    25  	"github.com/snapcore/snapd/overlord/auth"
    26  	"github.com/snapcore/snapd/overlord/snapstate"
    27  	"github.com/snapcore/snapd/overlord/state"
    28  	"github.com/snapcore/snapd/snap"
    29  )
    30  
    31  var (
    32  	appIconCmd = &Command{
    33  		Path:       "/v2/icons/{name}/icon",
    34  		GET:        appIconGet,
    35  		ReadAccess: openAccess{},
    36  	}
    37  )
    38  
    39  func appIconGet(c *Command, r *http.Request, user *auth.UserState) Response {
    40  	vars := muxVars(r)
    41  	name := vars["name"]
    42  
    43  	return iconGet(c.d.overlord.State(), name)
    44  }
    45  
    46  func iconGet(st *state.State, name string) Response {
    47  	st.Lock()
    48  	defer st.Unlock()
    49  
    50  	var snapst snapstate.SnapState
    51  	err := snapstate.Get(st, name, &snapst)
    52  	if err != nil {
    53  		if err == state.ErrNoState {
    54  			return SnapNotFound(name, err)
    55  		}
    56  		return InternalError("cannot consult state: %v", err)
    57  	}
    58  	sideInfo := snapst.CurrentSideInfo()
    59  	if sideInfo == nil {
    60  		return NotFound("snap has no current revision")
    61  	}
    62  
    63  	icon := snapIcon(snap.MinimalPlaceInfo(name, sideInfo.Revision))
    64  
    65  	if icon == "" {
    66  		return NotFound("local snap has no icon")
    67  	}
    68  
    69  	return fileResponse(icon)
    70  }