From d5b6e1d579dd629af3b700b60cda03415cf7fd32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B0=D1=82=D0=BE=D0=BB=D0=B8=D0=B9=20=D0=A2?= =?UTF-8?q?=D1=83=D1=85=D1=82=D0=B0=D1=80=D0=BE=D0=B2?= Date: Sat, 29 Jul 2023 10:19:42 +0500 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=20=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B0=D1=82=D1=8C=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 9 ++++++++ go.sum | 21 +++++++++++++++++++ picture.go | 40 +++++++++++++++++++++++------------ picture_test.go | 30 +++++++++++++++++++++++++++ seadb.go | 41 ++++++++++++++++++++++++++++++++++++ seadb_test.go | 22 ++++++++++++++++++++ weed.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ weed_test.go | 22 ++++++++++++++++++++ 8 files changed, 227 insertions(+), 13 deletions(-) create mode 100644 picture_test.go create mode 100644 seadb.go create mode 100644 seadb_test.go create mode 100644 weed.go create mode 100644 weed_test.go diff --git a/go.mod b/go.mod index 02ae96b..a8e1c4f 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,13 @@ go 1.19 require ( github.com/go-sql-driver/mysql v1.7.1 github.com/jmoiron/sqlx v1.3.5 + github.com/linxGnu/goseaweedfs v0.1.6 + github.com/stretchr/testify v1.8.4 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.5.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index aa856f1..fc93e0a 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,6 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= @@ -5,5 +8,23 @@ github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g= github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ= github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/linxGnu/goseaweedfs v0.1.6 h1:qcCz0aBJlXVCVX+NiEnU+uGHMBvJR33U3PquzlEpcs8= +github.com/linxGnu/goseaweedfs v0.1.6/go.mod h1:cv43dFeG4S6HnLa7U9aYw+wD/uZEi66UmwKdforYuWo= github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20220512140231-539c8e751b99/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/picture.go b/picture.go index 8d02dae..68ac63a 100644 --- a/picture.go +++ b/picture.go @@ -2,22 +2,20 @@ package pic import ( "fmt" + "strings" "github.com/jmoiron/sqlx" ) -//Picture работа с картинкаи +//Picture - работа с картинками type Picture struct { - ID int `json:"id"` - Url string `json:"src"` - Width int `json:"width"` - Height int `json:"height"` - Thumb string `json:"srct"` + ID int `db:"id" json:"id"` + Album string `db:"album" json:"album"` + Url string `db:"pic_url" json:"src"` + Width int `db:"pic_width" json:"width"` + Height int `db:"pic_height" json:"height"` + Thumb string `db:"thumb_url" json:"srct"` ThumbWidth int `db:"thumb_width" json:"imgtWidth"` ThumbHeight int `db:"thumb_height" json:"imgtHeight"` - - // закрытые поля - db *sqlx.DB - album string } //Hello - приветствие и комментарии к модулю @@ -26,7 +24,23 @@ func Hello() { fmt.Printf("v0.0.1 - начальная версия\n") } -func NewPicture(db *sqlx.DB, album string) Picture { - pic := Picture {db: db, album: album} +func NewPicture(album string) Picture { + pic := Picture {Album: album} return pic -} \ No newline at end of file +} + +// возвращает имя файла без расширения +func baseName(filename string) string { + // на всякий случай заменим обратный слэш + filename = strings.ReplaceAll(filename, "\\", "/") + + // проверка на наличие каталогов + items := strings.Split(filename, "/") + if len(items) > 0 { + // есть каталоги, уберем + filename = items[len(items)-1] + } + fn := strings.Split(filename, ".") + return fn[0] +} + diff --git a/picture_test.go b/picture_test.go new file mode 100644 index 0000000..bbd9ea6 --- /dev/null +++ b/picture_test.go @@ -0,0 +1,30 @@ +package pic + +import ( + //"log" + "testing" + "github.com/stretchr/testify/assert" +) + +func TestNewPicture(t *testing.T) { + // для тестирования базу данных не учитываем + p := NewPicture(nil, "проверка") + assert.Equal(t, p.Album, "проверка", "должны быть одинаковыми") +} + +func TestbaseName(t *testing.T) { + base := baseName("c:\\Проверка файла с каталогом\\test.jpg") + assert.Equal(t, base, "test", "должны быть одинаковыми") + + base = baseName("/Проверка файла с каталогом/test.jpg") + assert.Equal(t, base, "test", "должны быть одинаковыми") + + base = baseName("/test.jpg") + assert.Equal(t, base, "test", "должны быть одинаковыми") + + base = baseName("test.jpg") + assert.Equal(t, base, "test", "должны быть одинаковыми") + + base = baseName("test.jpg") + assert.NotEqual(t, base, "неверное имя", "должны быть разными") +} \ No newline at end of file diff --git a/seadb.go b/seadb.go new file mode 100644 index 0000000..4d4c31c --- /dev/null +++ b/seadb.go @@ -0,0 +1,41 @@ +package pic + +import ( +) + +//SeaDB - картинки в БД +type SeaDB struct { + db *sqlx.DB +} + +func NewSeaDB(db *qlx.DB) (*SeaDB, error) { + seadb := SeaDB{db: db} + return seadb +} + +//Возвращает список url-ов маленьких gif-ов их БД +func (sea *SeaDB)SmallGifs() ([]string, error) {} + cmd := ` + SELECT + newurl + FROM + filemeta + JOIN + pic ON filemeta.id = pic.file_id + WHERE + ext = '.gif' + and del = 0 + and a.converted = 0 + and b.width < 400 + LIMIT + 10; + ` + var urls []string + err := sea.db.Select(&urls, cmd) + if err != nil { + return nil, err + } + + return urls, nil +} + diff --git a/seadb_test.go b/seadb_test.go new file mode 100644 index 0000000..96b7d8c --- /dev/null +++ b/seadb_test.go @@ -0,0 +1,22 @@ +package pic + +import ( + //"log" + "testing" + "github.com/stretchr/testify/assert" +) + +func TestGetSmallGifs(t *testing.T) { + url, err := upload("weed_test.go") + assert.Nil(t, err) + //log.Printf("url: %s\n", url) + + + // проверка на обработку ошибок (перепутал url и fid) + err = delete("4,23456") + assert.NotNil(t, err) + + // проверка на корректный url + err = delete(url) + assert.Nil(t, err) +} diff --git a/weed.go b/weed.go new file mode 100644 index 0000000..92e48fc --- /dev/null +++ b/weed.go @@ -0,0 +1,55 @@ +package pic + +import ( + "net/http" + "time" + "strings" + "fmt" + //"log" + "github.com/linxGnu/goseaweedfs" +) + +var sw *goseaweedfs.Seaweed + +func init() { + masterURL := "http://192.168.0.105:9333" + filer := []string{} + sw, _ = goseaweedfs.NewSeaweed( + masterURL, // master server + filer, // нету у меня filer + 8096, // размер chunk + &http.Client{Timeout: 5 * time.Minute}) + + _, err := sw.Status() + if err != nil { + panic(err) + } +} + +// загружает файл filename на weed и возвращает url загруженного файла +func upload(filename string) (string, error) { + fid, err := sw.Submit(filename, "", "") + if err != nil { + return "", err + } + return fid.FileURL, nil +} + +// удаляет файл из weed +func delete(url string) error { + fid, err := getFID(url) + if err != nil { + return err + } + + err = sw.DeleteFile(fid, nil) + return err +} + +func getFID(url string) (string, error) { + items := strings.Split(url, "/") + if len(items) != 2 { + return "", fmt.Errorf("url должен быть в формате:
") + } + return items[len(items)-1], nil +} diff --git a/weed_test.go b/weed_test.go new file mode 100644 index 0000000..91bac7b --- /dev/null +++ b/weed_test.go @@ -0,0 +1,22 @@ +package pic + +import ( + //"log" + "testing" + "github.com/stretchr/testify/assert" +) + +func TestUpload(t *testing.T) { + url, err := upload("weed_test.go") + assert.Nil(t, err) + //log.Printf("url: %s\n", url) + + + // проверка на обработку ошибок (перепутал url и fid) + err = delete("4,23456") + assert.NotNil(t, err) + + // проверка на корректный url + err = delete(url) + assert.Nil(t, err) +}