Пишу конвертер и перебираюсь на linux
parent
239b05206a
commit
03c5af6536
2
go.mod
2
go.mod
|
@ -1,9 +1,11 @@
|
|||
module github.com/tiburon-777/OTUS_Project
|
||||
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v0.3.1
|
||||
github.com/amitrai48/logger v0.0.0-20190214092904-448001c055ec
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/stretchr/testify v1.6.1
|
||||
gopkg.in/yaml.v2 v2.3.0 // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -7,6 +7,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
|||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
|
|
|
@ -26,7 +26,7 @@ func New(conf config.Config) *App {
|
|||
|
||||
func (s *App) Start() error {
|
||||
s.Log.Infof("Server starting")
|
||||
s.Handler = loggingMiddleware(handler(&s.Cache), s.Log)
|
||||
s.Handler = loggingMiddleware(handler(s.Cache), s.Log)
|
||||
_ = s.ListenAndServe()
|
||||
s.Log.Infof("Server stoped")
|
||||
return nil
|
||||
|
|
|
@ -1,34 +1,48 @@
|
|||
package application
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/tiburon-777/OTUS_Project/previewer/models"
|
||||
"bytes"
|
||||
"image"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"errors"
|
||||
"image/jpeg"
|
||||
"github.com/nfnt/resize"
|
||||
)
|
||||
|
||||
func buildQuery(u *url.URL) (q models.Query, err error) {
|
||||
type Query struct {
|
||||
Height int
|
||||
Width int
|
||||
URL *url.URL
|
||||
}
|
||||
|
||||
func BuildQuery(u *url.URL) (q Query, err error) {
|
||||
t := strings.Split(u.Path, "/")
|
||||
q.Width,err=strconv.Atoi(t[2])
|
||||
if err!=nil {
|
||||
return models.Query{}, errors.New("width must be an integer")
|
||||
return Query{}, errors.New("width must be an integer")
|
||||
}
|
||||
q.Height,err=strconv.Atoi(t[3])
|
||||
if err!=nil {
|
||||
return models.Query{}, errors.New("height must be an integer")
|
||||
return Query{}, errors.New("height must be an integer")
|
||||
}
|
||||
tn := "http://"+strings.Join(t[4:],"/")
|
||||
q.URL,err=q.URL.Parse(tn)
|
||||
if err!=nil {
|
||||
return models.Query{}, errors.New("not valid url")
|
||||
return Query{}, errors.New("not valid url")
|
||||
}
|
||||
return q,nil
|
||||
}
|
||||
|
||||
func getPic(q models.Query) ([]byte,http.Header,error) {
|
||||
func (q Query) id() string {
|
||||
return strconv.Itoa(q.Width)+"/"+strconv.Itoa(q.Height)+"/"+q.URL.Path
|
||||
}
|
||||
|
||||
func (q Query) fromOrigin() ([]byte, http.Header, error) {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", "https://" + q.URL.Host + "/" + q.URL.Path, nil)
|
||||
if err != nil {
|
||||
|
@ -47,4 +61,19 @@ func getPic(q models.Query) ([]byte,http.Header,error) {
|
|||
return nil, nil, err
|
||||
}
|
||||
return body, res.Header, nil
|
||||
}
|
||||
|
||||
func (q Query) resize(b []byte) ([]byte, error) {
|
||||
i,_,err := image.Decode(bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Println("ресайзим")
|
||||
m := resize.Resize(uint(q.Width), uint(q.Height), i, resize.Bicubic)
|
||||
var g []byte
|
||||
s := bytes.NewBuffer(g)
|
||||
if err = jpeg.Encode(s,m,nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return g, nil
|
||||
}
|
|
@ -3,22 +3,36 @@ package application
|
|||
import (
|
||||
"github.com/tiburon-777/OTUS_Project/previewer/cache"
|
||||
"github.com/tiburon-777/OTUS_Project/previewer/logger"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func handler(c *cache.Cache) http.Handler {
|
||||
func handler(c cache.Cache) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
q,err := buildQuery(r.URL)
|
||||
q,err := BuildQuery(r.URL)
|
||||
if err!=nil {
|
||||
http.Error(w, "Can't parse query", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
pic, h, err := getPic(q)
|
||||
if err!=nil {
|
||||
http.Error(w, "Have problem with cache", http.StatusInternalServerError)
|
||||
b,ok := c.Get(cache.Key(q.id()))
|
||||
pic,ok := b.([]byte)
|
||||
if ok {
|
||||
log.Println("Взяли из кэша")
|
||||
writeResponce(w, nil,200,pic)
|
||||
return
|
||||
}
|
||||
pic,h,err := q.fromOrigin()
|
||||
if err != nil {
|
||||
http.Error(w, "Pic not found in origin", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
pic, err = q.resize(pic)
|
||||
if err != nil {
|
||||
http.Error(w, "Resizer kirdyk...", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
c.Set(cache.Key(q.id()),pic)
|
||||
writeResponce(w, h,200,pic)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
package models
|
||||
|
||||
import "net/url"
|
||||
|
||||
type Config struct {
|
||||
LruCapasity int
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
Height int
|
||||
Width int
|
||||
URL *url.URL
|
||||
}
|
Loading…
Reference in New Issue