Beispiel #1
0
func (op *qiankeOp) Init(config interface{}) (err error) {
	dlog.Println("qiankeOp init...")
	op.qiankeOpConfig = config.(*qiankeOpConfig)
	op.dbOp = new(QkDBOp)
	err = op.dbOp.Init(op.MysqlHost, op.MysqlUsername, op.MysqlPasswd, op.MysqlDbName)
	if err != nil {
		dlog.EPrintln(err.Error())
		return err
	}

	op.opReqPool = make(chan *qiankeRequest, op.RequestPoolSize)
	op.opHandlers = make(map[string]HandlerFunc, 0)
	for i := 0; i < op.RequestPoolSize; i++ {
		req := &qiankeRequest{}
		op.opReqPool <- req
	}

	op.register("login", loginHandler)
	op.register("register", registerHandler)

	dlog.Printf("qiankeOp init ok, config:%+v\n", op.qiankeOpConfig)
	return nil
}
Beispiel #2
0
func downloadHandler(foReq *FileRequest) {
	dlog.Println("downloadHandler in")

	w := foReq.out
	req := foReq.req
	fo := foReq.op
	values, err := url.ParseQuery(req.URL.RawQuery)
	if err != nil {
		dlog.EPrintln(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if reqUrl, ok := values["link"]; ok {
		if reqUrl[0] == "" {
			dlog.EPrintf("invalid link parameter,from remote %s, url:%s\n",
				req.RemoteAddr, req.URL.Path)
			http.Error(w, "invalid link parameter", http.StatusBadRequest)
			return
		}
		// realUrl, err := url.QueryUnescape(reqUrl[0])
		// if err != nil {
		// 	dlog.EPrintln(err.Error())
		// 	http.Error(w, err.Error(), http.StatusInternalServerError)
		// 	return
		// }
		hmResponse := fo.client.HMGet(Url2NameKey, reqUrl[0])
		result, err := hmResponse.Result()
		if err != nil {
			dlog.EPrintln(err.Error())
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		if len(result) == 0 || result[0] == nil {
			dlog.EPrintf("url %s not found!\n", reqUrl[0])
			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
			return
		}
		//just update url visit time.
		fo.client.ZAdd(UrlTimeKey, redis.Z{Score: float64(time.Now().Unix()), Member: reqUrl[0]})
		if len(result) == 0 || result[0] == nil {
			dlog.EPrintf("url2name failed, url:%s\n", reqUrl[0])
			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
			return
		}

		filename := result[0].(string)
		// fmt.Println(filename)
		urlRes, err := url.Parse(reqUrl[0])
		if err != nil {
			dlog.EPrintln(err.Error())
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		realFilename := ""
		if path.Ext(filename) == "" {
			realFilename = fmt.Sprintf("%s%s", filename, path.Ext(urlRes.Path))
		} else {
			realFilename = filename
		}
		w.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", realFilename))
		w.Header().Add("Content-Type", "application/octet-stream")
		fullFilePath := path.Join(foReq.op.StorePath, filename)
		if err = exists(fullFilePath); err != nil {
			fullFilePath = path.Join(foReq.op.StorePath, realFilename)
			if err = exists(fullFilePath); err != nil {
				dlog.EPrintf("file %s not found\n", fullFilePath)
				http.NotFound(w, req)
				return
			}
		}
		dlog.Printf("download file %s\n", fullFilePath)
		http.ServeFile(w, req, fullFilePath)
		//todo:
	} else {
		dlog.EPrintf("request bad parameter\n")
		http.Error(w, "bad parameter", http.StatusBadRequest)
	}
}