Skip to content

Commit

Permalink
feat: add support for scalable images and symlinked desktop files on …
Browse files Browse the repository at this point in the history
…appimages

Fixes #101
  • Loading branch information
srevinsaju committed Nov 22, 2022
1 parent 19350cc commit 55f917d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 577 deletions.
114 changes: 50 additions & 64 deletions appimage/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package appimage

import (
"fmt"
"github.com/gabriel-vasile/mimetype"
"image"
_ "image/png"
"io/ioutil"
Expand Down Expand Up @@ -66,35 +67,57 @@ func (appimage *AppImage) ExtractThumbnail(target string) {
return
}

baseIconName := fmt.Sprintf("%s.png", appimage.Executable)
targetIconPath := path.Join(target, baseIconName)
_, err = helpers.CopyFile(dirIcon, targetIconPath)
if err != nil {
logger.Warnf("copying thumbnail failed %s", err)
return
buf, err := os.Open(dirIcon)
logger.Debug("Trying to detect file type of the icon: supports .svg, .png")

// move to beginning of the DirIcon, since the image dimensions check might fail
// otherwise
mtype, err := mimetype.DetectReader(buf)
ext := "png"
if err != nil && os.Getenv("ZAP_IGNORE_MIMETYPE_CONFLICTS") != "1" {
logger.Fatal("Failed to detect file type from the .DirIcon image. Please create an issue on zap repository. Set ZAP_IGNORE_MIMETYPE_CONFLICTS=1 as environment variable to ignore thie error.")
} else if err != nil && os.Getenv("ZAP_IGNORE_MIMETYPE_CONFLICTS") == "1" {
logger.Warn("Failed retrieving mimetype from the .Diricon image, ignoring this error because ZAP_IGNORE_MIMETYPE_CONFLICTS is set as 1, on environment variables")
} else {
ext = mtype.Extension()
}

logger.Debugf("Trying to read image dimensions from %s", targetIconPath)
file, err := os.Open(targetIconPath)
if err != nil {
logger.Fatal(err)
buf.Seek(0, 0)
var im image.Config
if ext == "png" {
logger.Debugf("Trying to read image dimensions from %s", dirIcon)
if err != nil {
logger.Fatal(err)
}
logger.Debug("Decoding PNG image")
im, _, err = image.DecodeConfig(buf)
if err != nil {
logger.Warn(err)
return
}
}

logger.Debug("Decoding PNG image")
im, _, err := image.DecodeConfig(file)
err = buf.Close()
if err != nil {
logger.Warn(err)
logger.Warn("failed to close the icon file", err)
return
}

err = file.Close()
baseIconName := fmt.Sprintf("%s.%s", appimage.Executable, ext)

targetIconPath := path.Join(target, baseIconName)
_, err = helpers.CopyFile(dirIcon, targetIconPath)
if err != nil {
logger.Warn(err)
logger.Warnf("copying thumbnail failed %s", err)
return
}

logger.Debugf("Calculating symlink location")
xdgIconPath, err := xdg.DataFile(fmt.Sprintf("icons/hicolor/%dx%d/apps/", im.Width, im.Width))
var xdgIconPath string
if ext == "png" {
xdgIconPath, err = xdg.DataFile(fmt.Sprintf("icons/hicolor/%dx%d/apps/", im.Width, im.Width))
} else {
xdgIconPath, err = xdg.DataFile("icons/hicolor/scalable/apps/")
}
if err != nil {
logger.Warn(err)
return
Expand Down Expand Up @@ -145,6 +168,14 @@ func (appimage AppImage) Extract(dir string, relPath string) string {
}

dirIcon := path.Join(dir, "squashfs-root", relPath)
paths, err := filepath.Glob(dirIcon)
if err != nil {
panic(err)
}
if len(paths) == 0 {
logger.Fatal("Could not find any file matching pattern", relPath)
}
dirIcon = paths[0]

fileInfo, err := os.Lstat(dirIcon)
if os.IsNotExist(err) {
Expand Down Expand Up @@ -182,54 +213,9 @@ func (appimage AppImage) ExtractDesktopFile() ([]byte, error) {
defer os.RemoveAll(dir)

logger.Debug("Trying to extract Desktop files")
cmd := exec.Command(appimage.Filepath, "--appimage-extract", "*.desktop")
cmd.Dir = dir

err = cmd.Run()
output, _ := cmd.Output()
if string(output) != "" {
logger.Debugf("%s --appimage-extract *.desktop gave '%s'", appimage.Filepath, string(output))
}

if err != nil {
logger.Debugf("%s --appimage-extract *.Desktop failed with %s.", appimage.Filepath, err)
return []byte{}, err
}

squashfsDir, err := filepath.Abs(path.Join(dir, "squashfs-root"))
if err != nil {
logger.Debugf("Failed to get absolute path to squashfs-root, %s", err)
return nil, nil
}
logger.Debugf("Setting squashfs-root's abs path, %s", squashfsDir)

var desktopFiles []string
err = filepath.Walk(squashfsDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
desktopFile := appimage.Extract(dir, "*.desktop")

logger.Debugf("Checking if %s is a desktop file", path)
if strings.HasSuffix(path, ".desktop") {
logger.Debugf("Check for %s as desktop file -> passed", path)
desktopFiles = append(desktopFiles, path)
}
return nil
})
if err != nil {
logger.Warnf("Failed to walk through squashfs, %s", err)
return nil, nil
}

// couldn't find a desktop file
if len(desktopFiles) == 0 {
logger.Debug("Couldn't find a single desktop file")
return nil, nil
}
data, err := ioutil.ReadFile(desktopFiles[0])
data, err := ioutil.ReadFile(desktopFile)
if err != nil {
logger.Warnf("Reading desktop file failed %s", err)
return []byte{}, err
Expand Down
9 changes: 2 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@ go 1.15

require (
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/Songmu/gocredits v0.2.0 // indirect
github.com/adrg/xdg v0.4.0
github.com/buger/jsonparser v1.1.1
github.com/fatih/color v1.13.0
github.com/gabriel-vasile/mimetype v1.4.1
github.com/gen2brain/beeep v0.0.0-20220909211152-5a9ec94374f6
github.com/golangci/golangci-lint v1.35.2 // indirect
github.com/google/go-github/v31 v31.0.0
github.com/google/go-querystring v1.1.0 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/gopherjs/gopherwasm v1.1.0 // indirect
github.com/goreleaser/goreleaser v0.149.0 // indirect
github.com/kr/pty v1.1.4 // indirect
github.com/ktr0731/go-fuzzyfinder v0.7.0
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/schollz/progressbar/v3 v3.11.0
github.com/smartystreets/assertions v1.0.0 // indirect
github.com/srevinsaju/appimage-update v0.1.5-ss2
github.com/urfave/cli/v2 v2.20.3
github.com/webview/webview v0.0.0-20210330151455-f540d88dde4e
github.com/withmandala/go-log v0.1.0
golang.org/x/crypto v0.1.0 // indirect
gopkg.in/ini.v1 v1.67.0
k8s.io/utils v0.0.0-20221012122500-cfd413dd9e85
)
Loading

0 comments on commit 55f917d

Please sign in to comment.