File: file.go

package info (click to toggle)
amfora 1.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,272 kB
  • sloc: python: 71; sh: 42; makefile: 39
file content (126 lines) | stat: -rw-r--r-- 3,123 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package display

import (
	"fmt"
	"io/ioutil"
	"mime"
	"net/url"
	"os"
	"path/filepath"
	"strings"

	"github.com/makeworld-the-better-one/amfora/renderer"
	"github.com/makeworld-the-better-one/amfora/structs"
	"github.com/spf13/viper"
)

// handleFile handles urls using file:// protocol
func handleFile(u string) (*structs.Page, bool) {
	page := &structs.Page{}

	uri, err := url.ParseRequestURI(u)
	if err != nil {
		Error("File Error", "Cannot parse URI: "+err.Error())
		return page, false
	}
	fi, err := os.Stat(uri.Path)
	if err != nil {
		Error("File Error", "Cannot open local file: "+err.Error())
		return page, false
	}

	switch mode := fi.Mode(); {
	case mode.IsDir():
		// Must end in slash
		if u[len(u)-1] != '/' {
			u += "/"
		}
		for _, index := range []string{"index.gmi", "index.gemini"} {
			m, err := os.Stat(uri.Path + "/" + index)
			if err == nil && !m.IsDir() {
				return handleFile(u + index)
			}
		}
		return createDirectoryListing(u)
	case mode.IsRegular():
		if fi.Size() > viper.GetInt64("a-general.page_max_size") {
			Error("File Error", "Cannot open local file, exceeds page max size")
			return page, false
		}

		mimetype := mime.TypeByExtension(filepath.Ext(uri.Path))
		if strings.HasSuffix(u, ".gmi") || strings.HasSuffix(u, ".gemini") {
			mimetype = "text/gemini"
		}

		if !strings.HasPrefix(mimetype, "text/") {
			Error("File Error", "Cannot open file, not recognized as text.")
			return page, false
		}

		content, err := ioutil.ReadFile(uri.Path)
		if err != nil {
			Error("File Error", "Cannot open local file: "+err.Error())
			return page, false
		}

		if mimetype == "text/gemini" {
			rendered, links := renderer.RenderGemini(string(content), textWidth(), false)
			page = &structs.Page{
				Mediatype: structs.TextGemini,
				URL:       u,
				Raw:       string(content),
				Content:   rendered,
				Links:     links,
				TermWidth: termW,
			}
		} else {
			page = &structs.Page{
				Mediatype: structs.TextPlain,
				URL:       u,
				Raw:       string(content),
				Content:   renderer.RenderPlainText(string(content)),
				Links:     []string{},
				TermWidth: termW,
			}
		}
	}
	return page, true
}

// createDirectoryListing creates a text/gemini page for a directory
// that lists all the files as links.
func createDirectoryListing(u string) (*structs.Page, bool) {
	page := &structs.Page{}

	uri, err := url.ParseRequestURI(u)
	if err != nil {
		Error("Directory Error", "Cannot parse URI: "+err.Error())
	}

	files, err := ioutil.ReadDir(uri.Path)
	if err != nil {
		Error("Directory error", "Cannot open local directory: "+err.Error())
		return page, false
	}
	content := "Index of " + uri.Path + "\n"
	content += "=> ../ ../\n"
	for _, f := range files {
		separator := ""
		if f.IsDir() {
			separator = "/"
		}
		content += fmt.Sprintf("=> %s%s %s%s\n", f.Name(), separator, f.Name(), separator)
	}

	rendered, links := renderer.RenderGemini(content, textWidth(), false)
	page = &structs.Page{
		Mediatype: structs.TextGemini,
		URL:       u,
		Raw:       content,
		Content:   rendered,
		Links:     links,
		TermWidth: termW,
	}
	return page, true
}