Skip to content

Commit 7a5e46c

Browse files
committed
bugfix file close for windows
1 parent ac362f3 commit 7a5e46c

File tree

1 file changed

+13
-150
lines changed

1 file changed

+13
-150
lines changed

fileserver.go

Lines changed: 13 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/astaxie/beego/httplib"
3838
mapset "github.com/deckarep/golang-set"
3939
_ "github.com/eventials/go-tus"
40-
"github.com/garyburd/redigo/redis"
4140
jsoniter "github.com/json-iterator/go"
4241
"github.com/nfnt/resize"
4342
"github.com/radovskyb/watcher"
@@ -161,7 +160,6 @@ const (
161160
"default_scene": "default",
162161
"是否显示目录": "默认显示,方便调试用,上线时请关闭",
163162
"show_dir": true,
164-
"redis配置": "用于保存文件元信息",
165163
"邮件配置": "",
166164
"mail": {
167165
"user": "[email protected]",
@@ -186,8 +184,6 @@ const (
186184
"enable_migrate": false,
187185
"文件是否去重": "默认去重",
188186
"enable_distinct_file": true,
189-
"图片是否缩放": "默认是",
190-
"enable_image_resize": true,
191187
"是否开启跨站访问": "默认开启",
192188
"enable_cross_origin": true,
193189
"是否开启Google认证,实现安全的上传、下载": "默认不开启",
@@ -222,19 +218,8 @@ type Server struct {
222218
lockMap *goutil.CommonMap
223219
sceneMap *goutil.CommonMap
224220
searchMap *goutil.CommonMap
225-
rp *redis.Pool
226221
curDate string
227222
host string
228-
routers map[*regexp.Regexp]func(http.ResponseWriter, *http.Request)
229-
}
230-
type Redis struct {
231-
Address string `json:"address"`
232-
Pwd string `json:"pwd"`
233-
MaxIdle int `json:"maxIdle"`
234-
MaxActive int `json:"maxActive"`
235-
IdleTimeout int `json:"idleTimeout"`
236-
ConnectTimeout int `json:"connectTimeout"`
237-
DB int `json:"db"`
238223
}
239224
type FileInfo struct {
240225
Name string `json:"name"`
@@ -317,7 +302,6 @@ type GloablConfig struct {
317302
EnableMigrate bool `json:"enable_migrate"`
318303
EnableDistinctFile bool `json:"enable_distinct_file"`
319304
ReadOnly bool `json:"read_only"`
320-
EnableImageResize bool `json:"enable_image_resize"`
321305
EnableCrossOrigin bool `json:"enable_cross_origin"`
322306
EnableGoogleAuth bool `json:"enable_google_auth"`
323307
AuthUrl string `json:"auth_url"`
@@ -338,8 +322,6 @@ type GloablConfig struct {
338322
RetryCount int `json:"retry_count"`
339323
SyncDelay int64 `json:"sync_delay"`
340324
WatchChanSize int `json:"watch_chan_size"`
341-
Redis Redis `json:"redis"`
342-
EnableRedis bool `json:"enable_redis"`
343325
}
344326
type FileInfoResult struct {
345327
Name string `json:"name"`
@@ -370,7 +352,6 @@ func NewServer() *Server {
370352
queueFileLog: make(chan *FileLog, CONST_QUEUE_SIZE),
371353
queueUpload: make(chan WrapReqResp, 100),
372354
sumMap: goutil.NewCommonMap(365 * 3),
373-
routers: make(map[*regexp.Regexp]func(http.ResponseWriter, *http.Request)),
374355
}
375356

376357
defaultTransport := &http.Transport{
@@ -506,10 +487,6 @@ func (this *Server) BackUpMetaDataByDate(date string) {
506487
if _, err = fileMeta.WriteString(msg); err != nil {
507488
log.Error(err)
508489
}
509-
if Config().EnableRedis {
510-
this.SaveFileInfoToRedis(fileInfo.Md5, &fileInfo)
511-
this.SaveFileInfoToRedis(this.util.MD5(fileInfo.Path+"/"+name), &fileInfo)
512-
}
513490
msg = fmt.Sprintf("%s|%d|%d|%s\n", fileInfo.Md5, fileInfo.Size, fileInfo.TimeStamp, fileInfo.Path+"/"+name)
514491
if _, err = fileLog.WriteString(msg); err != nil {
515492
log.Error(err)
@@ -1242,9 +1219,7 @@ func (this *Server) DownloadNormalFileByURI(w http.ResponseWriter, r *http.Reque
12421219
}
12431220
fullpath, _ := this.GetFilePathFromRequest(w, r)
12441221
if imgWidth != 0 || imgHeight != 0 {
1245-
if Config().EnableImageResize {
1246-
this.ResizeImage(w, fullpath, uint(imgWidth), uint(imgHeight))
1247-
}
1222+
this.ResizeImage(w, fullpath, uint(imgWidth), uint(imgHeight))
12481223
return true, nil
12491224
}
12501225
staticHandler.ServeHTTP(w, r)
@@ -1379,25 +1354,11 @@ func (this *Server) ResizeImageByBytes(w http.ResponseWriter, data []byte, width
13791354
}
13801355
func (this *Server) ResizeImage(w http.ResponseWriter, fullpath string, width, height uint) {
13811356
var (
1382-
img image.Image
1383-
err error
1384-
imgType string
1385-
file *os.File
1386-
smallFile *os.File
1387-
resizeFileName string
1388-
resizePath string
1357+
img image.Image
1358+
err error
1359+
imgType string
1360+
file *os.File
13891361
)
1390-
resizePath = path.Dir(fullpath)
1391-
resizeFileName = "cache_" + this.util.MD5(fmt.Sprintf("%s?w=%d&h=%d", fullpath, width, height))
1392-
resizePath = fmt.Sprintf("%s/%s", resizePath, resizeFileName)
1393-
if this.util.IsExist(resizePath) {
1394-
if smallFile, err = os.Open(resizePath); err != nil {
1395-
log.Error(err)
1396-
}
1397-
defer smallFile.Close()
1398-
io.Copy(w, smallFile)
1399-
return
1400-
}
14011362
file, err = os.Open(fullpath)
14021363
if err != nil {
14031364
log.Error(err)
@@ -1408,24 +1369,16 @@ func (this *Server) ResizeImage(w http.ResponseWriter, fullpath string, width, h
14081369
log.Error(err)
14091370
return
14101371
}
1411-
defer file.Close()
1412-
if smallFile, err = os.Create(resizePath); err != nil {
1413-
log.Error(err)
1414-
return
1415-
}
1416-
defer smallFile.Close()
1372+
file.Close()
14171373
img = resize.Resize(width, height, img, resize.Lanczos3)
14181374
if imgType == "jpg" || imgType == "jpeg" {
1419-
jpeg.Encode(smallFile, img, nil)
1375+
jpeg.Encode(w, img, nil)
14201376
} else if imgType == "png" {
1421-
png.Encode(smallFile, img)
1377+
png.Encode(w, img)
14221378
} else {
14231379
file.Seek(0, 0)
14241380
io.Copy(w, file)
1425-
return
14261381
}
1427-
smallFile.Seek(0, 0)
1428-
io.Copy(w, smallFile)
14291382
}
14301383
func (this *Server) GetServerURI(r *http.Request) string {
14311384
return fmt.Sprintf("http://%s/", r.Host)
@@ -1826,28 +1779,12 @@ func (this *Server) Sync(w http.ResponseWriter, r *http.Request) {
18261779
func (this *Server) IsExistFromLevelDB(key string, db *leveldb.DB) (bool, error) {
18271780
return db.Has([]byte(key), nil)
18281781
}
1829-
func (this *Server) redisDo(action string, args ...interface{}) (reply interface{}, err error) {
1830-
c := this.rp.Get()
1831-
defer c.Close()
1832-
return c.Do(action, args...)
1833-
}
18341782
func (this *Server) GetFileInfoFromLevelDB(key string) (*FileInfo, error) {
18351783
var (
18361784
err error
18371785
data []byte
18381786
fileInfo FileInfo
1839-
fiJson string
18401787
)
1841-
if Config().EnableRedis {
1842-
if fiJson, err = redis.String(this.redisDo("GET", key)); err != nil {
1843-
return nil, err
1844-
} else {
1845-
if err = json.Unmarshal([]byte(fiJson), &fileInfo); err != nil {
1846-
return nil, err
1847-
}
1848-
return &fileInfo, nil
1849-
}
1850-
}
18511788
if data, err = this.ldb.Get([]byte(key), nil); err != nil {
18521789
return nil, err
18531790
}
@@ -1889,19 +1826,6 @@ func (this *Server) RemoveKeyFromLevelDB(key string, db *leveldb.DB) error {
18891826
err = db.Delete([]byte(key), nil)
18901827
return err
18911828
}
1892-
func (this *Server) SaveFileInfoToRedis(key string, fileInfo *FileInfo) (*FileInfo, error) {
1893-
var (
1894-
err error
1895-
data []byte
1896-
)
1897-
if data, err = json.Marshal(fileInfo); err != nil {
1898-
return fileInfo, err
1899-
}
1900-
if _, err = this.redisDo("SET", key, string(data)); err != nil {
1901-
return nil, err
1902-
}
1903-
return fileInfo, nil
1904-
}
19051829
func (this *Server) SaveFileInfoToLevelDB(key string, fileInfo *FileInfo, db *leveldb.DB) (*FileInfo, error) {
19061830
var (
19071831
err error
@@ -1921,11 +1845,6 @@ func (this *Server) SaveFileInfoToLevelDB(key string, fileInfo *FileInfo, db *le
19211845
logKey := fmt.Sprintf("%s_%s_%s", logDate, CONST_FILE_Md5_FILE_NAME, fileInfo.Md5)
19221846
this.logDB.Put([]byte(logKey), data, nil)
19231847
}
1924-
if Config().EnableRedis && db == this.ldb {
1925-
if _, err = this.redisDo("SET", key, string(data)); err != nil {
1926-
log.Error(err)
1927-
}
1928-
}
19291848
return fileInfo, nil
19301849
}
19311850
func (this *Server) IsPeer(r *http.Request) bool {
@@ -2462,7 +2381,6 @@ func (this *Server) Upload(w http.ResponseWriter, r *http.Request) {
24622381
}
24632382
}
24642383
fn = folder + "/" + this.util.GetUUID()
2465-
24662384
fpTmp, err = os.OpenFile(fn, os.O_RDWR|os.O_CREATE, 0777)
24672385
if err != nil {
24682386
log.Error(err)
@@ -2485,6 +2403,8 @@ func (this *Server) Upload(w http.ResponseWriter, r *http.Request) {
24852403
fpBody.Close()
24862404
os.Remove(fn)
24872405
}()
2406+
fpBody, err = os.Open(fn)
2407+
r.Body = fpBody
24882408
done := make(chan bool, 1)
24892409
this.queueUpload <- WrapReqResp{&w, r, done}
24902410
<-done
@@ -4264,60 +4184,6 @@ func (this *Server) FormatStatInfo() {
42644184
this.RepairStatByDate(this.util.GetToDay())
42654185
}
42664186
}
4267-
4268-
func (this *Server) initRedis() *redis.Pool {
4269-
4270-
pool := &redis.Pool{
4271-
MaxIdle: Config().Redis.MaxIdle,
4272-
MaxActive: Config().Redis.MaxActive,
4273-
IdleTimeout: time.Duration(Config().Redis.IdleTimeout) * time.Second,
4274-
Wait: true,
4275-
Dial: func() (redis.Conn, error) {
4276-
conn, err := redis.Dial("tcp", Config().Redis.Address,
4277-
redis.DialConnectTimeout(time.Duration(Config().Redis.ConnectTimeout)*time.Second),
4278-
redis.DialPassword(Config().Redis.Pwd),
4279-
redis.DialDatabase(Config().Redis.DB),
4280-
)
4281-
if err != nil {
4282-
fmt.Println(err)
4283-
log.Error(err)
4284-
}
4285-
return conn, err
4286-
4287-
},
4288-
TestOnBorrow: func(c redis.Conn, t time.Time) error {
4289-
_, err := c.Do("ping")
4290-
if err != nil {
4291-
log.Error(err)
4292-
return err
4293-
}
4294-
return err
4295-
},
4296-
}
4297-
return pool
4298-
}
4299-
func (this *Server) Routing(w http.ResponseWriter, r *http.Request) {
4300-
realPath := r.URL.Path
4301-
for re, service := range this.routers {
4302-
if m := re.FindStringSubmatch(realPath); m != nil {
4303-
//p := re.String()
4304-
//if strings.Index(p, STATIC_DIR_NAME) != -1 {
4305-
// p = p[0 : strings.LastIndex(p, "/")+1]
4306-
// p = strings.Replace(realPath, p, "", 1) //realPath /group1/static/uppy.html
4307-
// if b, e := Asset(p); e == nil { // p uppy.html
4308-
// w.Write(b)
4309-
// return
4310-
// }
4311-
//}
4312-
service(w, r)
4313-
return
4314-
}
4315-
}
4316-
this.Download(w, r)
4317-
}
4318-
func (this *Server) RegisterRouting(pattern *regexp.Regexp, handler func(http.ResponseWriter, *http.Request)) {
4319-
this.routers[pattern] = handler
4320-
}
43214187
func (this *Server) initComponent(isReload bool) {
43224188
var (
43234189
ip string
@@ -4363,9 +4229,6 @@ func (this *Server) initComponent(isReload bool) {
43634229
this.sceneMap.Put(kv[0], kv[1])
43644230
}
43654231
}
4366-
if Config().EnableRedis {
4367-
this.rp = this.initRedis()
4368-
}
43694232
if Config().ReadTimeout == 0 {
43704233
Config().ReadTimeout = 60 * 10
43714234
}
@@ -4472,8 +4335,10 @@ func (this *Server) Start() {
44724335
}()
44734336
uploadPage := "upload.html"
44744337
if groupRoute == "" {
4338+
http.HandleFunc(fmt.Sprintf("%s", "/"), this.Download)
44754339
http.HandleFunc(fmt.Sprintf("/%s", uploadPage), this.Index)
44764340
} else {
4341+
http.HandleFunc(fmt.Sprintf("%s", "/"), this.Download)
44774342
http.HandleFunc(fmt.Sprintf("%s", groupRoute), this.Download)
44784343
http.HandleFunc(fmt.Sprintf("%s/%s", groupRoute, uploadPage), this.Index)
44794344
}
@@ -4499,9 +4364,7 @@ func (this *Server) Start() {
44994364
http.HandleFunc(fmt.Sprintf("%s/receive_md5s", groupRoute), this.ReceiveMd5s)
45004365
http.HandleFunc(fmt.Sprintf("%s/gen_google_secret", groupRoute), this.GenGoogleSecret)
45014366
http.HandleFunc(fmt.Sprintf("%s/gen_google_code", groupRoute), this.GenGoogleCode)
4502-
http.HandleFunc(fmt.Sprintf("%s", "/"), this.Routing) //default handler
4503-
http.Handle(fmt.Sprintf("%s/%s/", groupRoute, STATIC_DIR_NAME), http.StripPrefix(fmt.Sprintf("%s/%s/", groupRoute, STATIC_DIR_NAME), http.FileServer(http.Dir(STATIC_DIR_NAME))))
4504-
//server.RegisterRouting(regexp.MustCompile(fmt.Sprintf("%s/%s/.*", groupRoute, STATIC_DIR_NAME)), http.FileServer(http.Dir(STATIC_DIR_NAME)).ServeHTTP)
4367+
http.Handle(fmt.Sprintf("%s/static/", groupRoute), http.StripPrefix(fmt.Sprintf("%s/static/", groupRoute), http.FileServer(http.Dir("./static"))))
45054368
http.HandleFunc("/"+Config().Group+"/", this.Download)
45064369
fmt.Println("Listen on " + Config().Addr)
45074370
if Config().EnableHttps {

0 commit comments

Comments
 (0)