-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagesrv.go
More file actions
140 lines (118 loc) · 4.03 KB
/
imagesrv.go
File metadata and controls
140 lines (118 loc) · 4.03 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*---------------------------------------------------------------------+
| Copyright (c) 2021 by Mark W. Kernodle
|
| FILE: imagesrv.go
| PURPOSE: Serve up output of tree and file command for a directory in HTML
|
| NOTES: For NetApp coding challenge 11/28/2021
+--------------------------------------------------------------------*/
package main
import (
"log"
"net/http"
"os"
"fmt"
"os/exec"
"os/signal"
"syscall"
"bytes"
"strings"
"github.com/gorilla/mux"
)
const theTitle string = "NetApp tree for candidate files"
func main() {
var theLines [][]byte
var theLine string
var err error
var fragment string
var muxIt = mux.NewRouter()
var scanDir string
var extens [3]string
// Parse arguments to get the directory to browse.
// Copy image tree info a temp directory, and then construct
// an index.html comprising only the image files.
// Then serve up this file with the FileServer abstraction.
scanDir = os.Args[1]
extens[0] = ".jpg"
extens[1] = ".png"
extens[2] = ".gif"
tmpDir,err := os.MkdirTemp("/var/tmp", "NetApp-test")
if err != nil {
log.Fatal(err)
}
// We must copy the source tree into place due to a limitation of
// path routes from '/' in http.Fileserver. Probably better to have added
// a custome Handler to surmount this restriction.
srcDir := scanDir
theCmd := exec.Command("cp", "-R", srcDir, tmpDir)
_,err = theCmd.Output()
if err != nil {
log.Fatal(err)
}
theIndex := tmpDir + "/index.html"
indexFile,err := os.Create(theIndex)
if err != nil {
log.Fatal(err)
}
preDir := strings.TrimPrefix(scanDir, "/var/tmp")
srcDir = tmpDir + "/" + preDir
theCmd = exec.Command("tree", "-i", "-T", theTitle, "-h", "--du", "-H", preDir, srcDir)
treeOut,err := theCmd.Output()
if err != nil {
log.Fatal(err)
}
theLines = bytes.Split(treeOut, []byte("\n"))
for i,_ := range theLines {
theLine = string(theLines[i])
if theLine != "" {
fragment = fmt.Sprintf("%s\n", theLine)
gotIt := strings.Contains(fragment,extens[0])
if gotIt == false {
gotIt = strings.Contains(fragment,extens[1])
}
if gotIt == false {
gotIt = strings.Contains(fragment,extens[2])
}
if gotIt {
hrefMark := strings.Index(fragment,"href=")
fileN := fragment[hrefMark + 6:]
fileL := strings.Index(fileN,"\"")
fName := fileN[:fileL]
fName = tmpDir + fName
// sanitize blank characters that have been subsumed with %20
fName = strings.Replace (fName, "%20", " ", -1)
theCmd:= exec.Command("file", "-b", fName)
fileCmdOut,err := theCmd.Output()
if err != nil {
log.Fatal(err)
}
fileCmdString := string(fileCmdOut)
fileCmdString = strings.TrimRight(fileCmdString,"\n")
snippet := " - " + "<small>" + fileCmdString + "</small>" + "<br>"
enhanced := strings.Replace(fragment, "<br>", snippet, 1)
bytesWritten, err := indexFile.Write([]byte(enhanced))
if err != nil || bytesWritten <= 0 {
log.Fatal(err)
}
} else {
bytesWritten, err := indexFile.Write([]byte(theLine))
if err != nil || bytesWritten <= 0 {
log.Fatal(err)
}
}
}
}
indexFile.Close()
imgDir := http.Dir(tmpDir)
fileViewer := http.FileServer(imgDir)
// Break out of the Listen/Server loop upon ctrl-c
ourC := make(chan os.Signal)
signal.Notify(ourC, os.Interrupt, syscall.SIGTERM)
go func() {
<-ourC
os.RemoveAll(tmpDir)
os.Exit(0)
}()
muxIt.PathPrefix("/").Handler(http.StripPrefix("/", fileViewer))
log.Fatal(http.ListenAndServe(":9080", muxIt))
}