Commit 6a5c6931ebe949e22aada7d63497bcff1ac6c402

Authored by Marko Tikvić
1 parent 2c237577d3
Exists in master

added database map to server

document/document.go
... ... @@ -7,7 +7,6 @@ import (
7 7 "mime"
8 8 "net/http"
9 9 "os"
10   - "strings"
11 10 "time"
12 11  
13 12 web "git.to-net.rs/marko.tikvic/webutility"
... ... @@ -49,7 +48,7 @@ func OpenFileAsDocument(path string) (*Document, error) {
49 48  
50 49 d.FileName = stats.Name()
51 50 d.Size = stats.Size()
52   - d.Extension = FileExtension(d.FileName)
  51 + d.Extension = web.FileExtension(d.FileName)
53 52  
54 53 d.data = make([]byte, d.Size)
55 54 if _, err = f.Read(d.data); err != nil {
... ... @@ -99,18 +98,6 @@ func ServeDocument(w http.ResponseWriter, d *Document, downloadPrompt bool) erro
99 98 return nil
100 99 }
101 100  
102   -// FileExists ...
103   -func FileExists(path string) bool {
104   - temp, err := os.Open(path)
105   - defer temp.Close()
106   -
107   - if err != nil {
108   - return false
109   - }
110   -
111   - return true
112   -}
113   -
114 101 // ParseDocument ...
115 102 func ParseDocument(req *http.Request) (doc *Document, err error) {
116 103 req.ParseMultipartForm(32 << 20)
... ... @@ -127,7 +114,7 @@ func ParseDocument(req *http.Request) (doc *Document, err error) {
127 114 fsize := fheader.Size
128 115 ftype := fmt.Sprintf("%v", fheader.Header["Content-Type"][0])
129 116  
130   - fextn := FileExtension(fname)
  117 + fextn := web.FileExtension(fname)
131 118 if fextn == "" {
132 119 return doc, errors.New("invalid extension")
133 120 }
... ... @@ -155,31 +142,16 @@ func ParseDocument(req *http.Request) (doc *Document, err error) {
155 142 return doc, nil
156 143 }
157 144  
158   -// DirectoryFromPath ...
159   -func DirectoryFromPath(path string) (dir string) {
160   - parts := strings.Split(path, "/")
161   - if len(parts) == 1 {
162   - return ""
163   - }
164   -
165   - dir = parts[0]
166   - for _, p := range parts[1 : len(parts)-1] {
167   - dir += "/" + p
168   - }
169   -
170   - return dir
171   -}
172   -
173 145 // SaveToFile ...
174 146 func (d *Document) SaveToFile(path string) (f *os.File, err error) {
175 147 d.Path = path
176 148  
177   - if FileExists(path) {
  149 + if web.FileExists(path) {
178 150 err = fmt.Errorf("file %s alredy exists", path)
179 151 return nil, err
180 152 }
181 153  
182   - if parentDir := DirectoryFromPath(path); parentDir != "" {
  154 + if parentDir := web.DirectoryFromPath(path); parentDir != "" {
183 155 if err = os.MkdirAll(parentDir, os.ModePerm); err != nil {
184 156 if !os.IsExist(err) {
185 157 return nil, err
... ... @@ -201,10 +173,6 @@ func (d *Document) SaveToFile(path string) (f *os.File, err error) {
201 173 return f, nil
202 174 }
203 175  
204   -func DeleteFile(path string) error {
205   - return os.Remove(path)
206   -}
207   -
208 176 func DeleteDocuments(docs []*Document) error {
209 177 for _, d := range docs {
210 178 if err := d.DeleteFile(); err != nil {
... ... @@ -218,11 +186,3 @@ func DeleteDocuments(docs []*Document) error {
218 186 func (d *Document) DeleteFile() error {
219 187 return os.Remove(d.Path)
220 188 }
221   -
222   -func FileExtension(path string) string {
223   - parts := strings.Split(path, ".") // because name can contain dots
224   - if len(parts) < 2 {
225   - return ""
226   - }
227   - return "." + parts[len(parts)-1]
228   -}
... ...
... ... @@ -2,6 +2,7 @@ package webutility
2 2  
3 3 import (
4 4 "bytes"
  5 + "fmt"
5 6 "io"
6 7 "io/ioutil"
7 8 "os"
... ... @@ -81,6 +82,48 @@ func ListDir(path string) (fnames []string, err error) {
81 82 }
82 83  
83 84 func WorkingDir() string {
84   - dir, _ := os.Getwd()
  85 + path, err := os.Getwd()
  86 + if err != nil {
  87 + fmt.Printf("couldn't get working directory: %s\n", err.Error())
  88 + }
  89 + return path
  90 +}
  91 +
  92 +func FileExtension(path string) string {
  93 + parts := strings.Split(path, ".") // because name can contain dots
  94 + if len(parts) < 2 {
  95 + return ""
  96 + }
  97 + return "." + parts[len(parts)-1]
  98 +}
  99 +
  100 +func DeleteFile(path string) error {
  101 + return os.Remove(path)
  102 +}
  103 +
  104 +// DirectoryFromPath ...
  105 +func DirectoryFromPath(path string) (dir string) {
  106 + parts := strings.Split(path, "/")
  107 + if len(parts) == 1 {
  108 + return ""
  109 + }
  110 +
  111 + dir = parts[0]
  112 + for _, p := range parts[1 : len(parts)-1] {
  113 + dir += "/" + p
  114 + }
  115 +
85 116 return dir
86 117 }
  118 +
  119 +// FileExists ...
  120 +func FileExists(path string) bool {
  121 + temp, err := os.Open(path)
  122 + defer temp.Close()
  123 +
  124 + if err != nil {
  125 + return false
  126 + }
  127 +
  128 + return true
  129 +}
... ...
... ... @@ -82,3 +82,13 @@ func FormatFloat64Number(f float64, dec int) string {
82 82  
83 83 return res
84 84 }
  85 +
  86 +func MaxFlaot64(vars ...float64) (max float64) {
  87 + max = vars[0]
  88 + for _, v := range vars {
  89 + if v > max {
  90 + max = v
  91 + }
  92 + }
  93 + return max
  94 +}
... ...
... ... @@ -15,6 +15,7 @@ type Server struct {
15 15 Logger *gologger.Logger
16 16 Port string
17 17 UTCOffset int64
  18 + DBs map[string]*sql.DB
18 19 }
19 20  
20 21 func NewODBCServer(dsn, port, logDir string, utcOffset int64) (s *Server, err error) {
... ... @@ -34,6 +35,9 @@ func NewODBCServer(dsn, port, logDir string, utcOffset int64) (s *Server, err er
34 35  
35 36 s.UTCOffset = utcOffset
36 37  
  38 + s.DBs = make(map[string]*sql.DB)
  39 + s.DBs["default"] = s.DB
  40 +
37 41 return s, nil
38 42 }
39 43  
... ...