Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mediatypes support #134

Merged
merged 16 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ Features in *italics* are in the master branch, but not in the latest release.
- Disabled by default, enable in config
- [x] Proxying
- Schemes like Gopher or HTTP can be proxied through a Gemini server
- [x] *Configure applications to open particular mediatypes*
- [ ] Allow piping/streaming content instead of downloading it first
- [x] Client certificate support
- [ ] Full client certificate UX within the client
- Create transient and permanent certs within the client, per domain
Expand Down
60 changes: 60 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@ var bkmkDir string
var bkmkPath string

var DownloadsDir string
var TempDownloadsDir string

// Command for opening HTTP(S) URLs in the browser, from "a-general.http" in config.
var HTTPCommand []string

type MediaHandler struct {
Cmd []string
NoPrompt bool
}

var MediaHandlers = make(map[string]MediaHandler)

func Init() error {

// *** Set paths ***
Expand Down Expand Up @@ -169,6 +177,36 @@ func Init() error {
DownloadsDir = dDir
}

// Setup temporary downloads dir
if viper.GetString("a-general.temp_downloads") == "" {
TempDownloadsDir = filepath.Join(os.TempDir(), "amfora_temp")

// Make sure it exists
err = os.MkdirAll(TempDownloadsDir, 0755)
if err != nil {
return fmt.Errorf("downloads path could not be created: %s", DownloadsDir)
sudobash1 marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
// Validate path
dDir := viper.GetString("a-general.temp_downloads")
di, err := os.Stat(dDir)
if err == nil {
if !di.IsDir() {
return fmt.Errorf("temp downloads path specified is not a directory: %s", dDir)
}
} else if os.IsNotExist(err) {
// Try to create path
err = os.MkdirAll(dDir, 0755)
if err != nil {
return fmt.Errorf("temp downloads path could not be created: %s", dDir)
}
} else {
// Some other error
return fmt.Errorf("couldn't access temp downloads directory: %s", dDir)
}
TempDownloadsDir = dDir
}

// *** Setup vipers ***

TofuStore.SetConfigFile(tofuDBPath)
Expand Down Expand Up @@ -202,6 +240,7 @@ func Init() error {
viper.SetDefault("a-general.left_margin", 0.15)
viper.SetDefault("a-general.max_width", 100)
viper.SetDefault("a-general.downloads", "")
viper.SetDefault("a-general.temp_downloads", "")
viper.SetDefault("a-general.page_max_size", 2097152)
viper.SetDefault("a-general.page_max_time", 10)
viper.SetDefault("a-general.emoji_favicons", false)
Expand Down Expand Up @@ -249,5 +288,26 @@ func Init() error {
HTTPCommand = strings.Fields(viper.GetString("a-general.http"))
}

var rawMediaHandlers []struct {
Cmd []string `mapstructure:"cmd"`
Types []string `mapstructure:"types"`
NoPrompt bool `mapstructure:"no_prompt"`
}
err = viper.UnmarshalKey("mediatype-handlers", &rawMediaHandlers)
if err != nil {
return fmt.Errorf("couldn't parse mediatype-handlers section in config: %w", err)
}
for _, rawMediaHandler := range rawMediaHandlers {
for _, typ := range rawMediaHandler.Types {
if _, ok := MediaHandlers[typ]; ok {
return fmt.Errorf(`Multiple mediatype-handlers defined for %v`, typ)
sudobash1 marked this conversation as resolved.
Show resolved Hide resolved
}
MediaHandlers[typ] = MediaHandler{
Cmd: rawMediaHandler.Cmd,
NoPrompt: rawMediaHandler.NoPrompt,
}
}
}

return nil
}
38 changes: 38 additions & 0 deletions default-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,44 @@ shift_numbers = "!@#$%^&*()"
other = 'off'


# [[mediatype-handlers]]
# Specify what applications will open certain filetypes.
sudobash1 marked this conversation as resolved.
Show resolved Hide resolved
# By default your default application will be used to open the file when you select "Open".
sudobash1 marked this conversation as resolved.
Show resolved Hide resolved
#
# To open jpeg files with the feh command:
# [[mediatype-handlers]]
# cmd = ["feh"]
# types = ["image/jpeg"]
#
# Each command that you specify must come under its own [[mediatype-handlers]]. You may
# specify as many [[mediatype-handlers]] as you want to setup multiple commands.
#
# If the subtype is omitted then the specified command will be used for the
# entire type:
# [[mediatype-handlers]]
# command = ["vlc", "--flag"]
# types = ["audio", "video"]
#
# A catch-all handler can by specified with "*".
# Note that there are already catch-all handlers in place for all OSes,
# that open the file using your default application. This is only if you
# want to override that.
# [[mediatype-handlers]]
# cmd = ["some-command"]
# types = [
# "application/pdf",
# "*",
# ]
#
# If you want to always open a type in its viewer without the download or open
# prompt appearing, you can add no_prompt = true
#
# [[mediatype-handlers]]
# cmd = ["feh"]
# types = ["image"]
# no_prompt = true
sudobash1 marked this conversation as resolved.
Show resolved Hide resolved


[cache]
# Options for page cache - which is only for text/gemini pages
# Increase the cache size to speed up browsing at the expense of memory
Expand Down
Loading