-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashreplacer.go
93 lines (84 loc) · 2.28 KB
/
hashreplacer.go
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
/***********************************************************************
MicroCore
Copyright 2020 - 2020 by Danyil Dobryvechir (dobrivecher@yahoo.com ddobryvechir@gmail.com)
************************************************************************/
package dvmodules
import (
"errors"
"github.com/Dobryvechir/microcore/pkg/dvcom"
"github.com/Dobryvechir/microcore/pkg/dvmeta"
"io/ioutil"
"strings"
)
const (
StarKey = "StarKey"
)
func IsCorrectHash(s string) bool {
n := len(s)
if n == 0 {
return false
}
for i := 0; i < n; i++ {
c := s[i]
if !(c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F' || c >= '0' && c <= '9') {
return false
}
}
return true
}
func hashReplacerEndPointHandler(request *dvmeta.RequestContext) bool {
url := request.Url
finish := strings.LastIndex(url, ".")
start := strings.LastIndex(url[:finish], ".") + 1
if finish <= 0 || start <= 1 || !IsCorrectHash(url[start:finish]) || request.Server.BaseFolderUrl == "" {
return false
}
dirName := ""
prefix := url[:start]
suffix := url[finish:]
pos := strings.LastIndex(prefix, "/")
if pos >= 0 {
dirName := url[:pos]
if dirName != "" && dirName[0] != '/' {
dirName = "/" + dirName
}
prefix = prefix[pos+1:]
}
dirName = request.Server.BaseFolderUrl + dirName
files, err := ioutil.ReadDir(dirName)
if err != nil {
return false
}
for _, f := range files {
name := f.Name()
if strings.HasPrefix(name, prefix) && strings.HasSuffix(name, suffix) {
request.FileName = dirName + "/" + name
dvcom.HandleFromFile(request)
return true
}
}
return false
}
func hashReplacerServerInitHandler(params []string) (map[string]string, error) {
starLen := len(params)
if starLen != 1 {
return nil, errors.New("module hashReplacer requires 1 parameter")
}
replacerName := params[0]
if replacerName != "hash" {
return nil, errors.New("Only 'hash' is supported, but you specified " + replacerName)
}
res := make(map[string]string)
res[StarKey] = replacerName
return res, nil
}
var hashReplacerConfig = &RegistrationConfig{
Name: "hashReplacer",
EndPointHandler: hashReplacerEndPointHandler,
ServerInitHandler: hashReplacerServerInitHandler,
}
func registerHashReplacer() bool {
RegisterModule(hashReplacerConfig, false)
return true
}
var hashReplacerInited = registerHashReplacer()