-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.go
More file actions
151 lines (134 loc) · 4.22 KB
/
handlers.go
File metadata and controls
151 lines (134 loc) · 4.22 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
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"log"
"net/http"
"net/url"
"os"
"path/filepath"
srvConfig "github.com/CHESSComputing/golib/config"
server "github.com/CHESSComputing/golib/server"
"github.com/gin-gonic/gin"
)
// GET handlers
func DataLocationHandler(c *gin.Context) {
// Get DID from HTTP request
did := c.Query("did")
if did == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing did parameter"})
return
}
// the /data URL may contain additional path parameter
// it refers to sub-path within raw data location
spath := c.Query("path")
// if we got file parameter we know which file to fetch
fileName := c.Query("file")
// Find metadata record for given DID
meta, err := findMetaDataRecord(did)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "metadata record not found"})
return
}
// by default use DataLocationAttributes as list of meta-data record attributes to lookup
// but if HTTP request provide concrete attribute switch to it
locationAttributes := srvConfig.Config.CHESSMetaData.DataLocationAttributes
attr := c.Query("attr")
if attr != "" {
locationAttributes = []string{attr}
}
// Extract data location from metadata record
for _, attr := range locationAttributes {
if val, ok := meta[attr]; ok {
// if location attribute (raw data location) is found redirect to it
path := val.(string)
// join path from meta-data record with possible spath
if spath != "" {
path = filepath.Join(path, spath)
}
// if we have file name we should present it back to upstream caller
if fileName != "" {
fname := filepath.Join(path, fileName)
// Serve file content if it's a file
http.ServeFile(c.Writer, c.Request, fname)
return
}
// get info about our path
info, err := os.Stat(path)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "path not found"})
return
}
// If requesting JSON, return directory listing in JSON format
acceptHeader := c.GetHeader("Accept")
if info.IsDir() {
entries, err := getFileList(did, path, spath)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "cannot read directory"})
return
}
if acceptHeader == "application/json" {
c.JSON(http.StatusOK, entries)
return
}
// Render HTML template
tmpl := server.MakeTmpl(StaticFs, "DataManagement")
base := srvConfig.Config.DataManagement.WebServer.Base
tmpl["Base"] = base
tmpl["Area"] = path
tmpl["Entries"] = entries
tmpl["Did"] = did
tmpl["FileExtensions"] = fileExtensions(path)
content := server.TmplPage(StaticFs, "fs.tmpl", tmpl)
page := server.Header(StaticFs, base) + content + server.FooterEmpty(StaticFs, base)
c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(page))
return
}
// Serve file content if it's a file
http.ServeFile(c.Writer, c.Request, path)
return
}
}
c.JSON(http.StatusNotFound, gin.H{"error": "data location not found in metadata"})
}
// DataFilesHandler provides access to data files
func DataFilesHandler(c *gin.Context) {
// Get DID from HTTP request
did := c.Query("did")
if did == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing did parameter"})
return
}
pattern := c.Query("pattern")
if pattern == "" {
c.JSON(http.StatusNotFound, gin.H{"error": "no files pattern is provided"})
return
}
if val, err := url.QueryUnescape(did); err == nil {
did = val
}
if val, err := url.QueryUnescape(pattern); err == nil {
pattern = val
}
// Find metadata record for given DID
meta, err := findMetaDataRecord(did)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "metadata record not found"})
return
}
// Extract data location from metadata record
for _, attr := range srvConfig.Config.CHESSMetaData.DataLocationAttributes {
if val, ok := meta[attr]; ok {
// take data location
path := val.(string)
// find all files in that location using our pattern
files, err := findFiles(path, pattern)
if err != nil {
log.Println("WARNING: findFiles", err)
// c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
// return
}
c.JSON(http.StatusOK, files)
return
}
}
c.JSON(http.StatusNotFound, gin.H{"error": "data files not found"})
}