Compare commits
2 Commits
324d6e0d50
...
0d976f0197
| Author | SHA1 | Date | |
|---|---|---|---|
| 0d976f0197 | |||
| 3377e4a352 |
12
README.md
12
README.md
@@ -126,9 +126,14 @@ type Model struct {
|
|||||||
type Image struct {
|
type Image struct {
|
||||||
ID int `json:"id" gorm:"primary_key"`
|
ID int `json:"id" gorm:"primary_key"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
Hash string `json:"hash"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Size int `json:"size"`
|
||||||
Width int `json:"width"`
|
Width int `json:"width"`
|
||||||
Height int `json:"height"`
|
Height int `json:"height"`
|
||||||
Prompt string `json:"prompt"`
|
Prompt string `json:"prompt"`
|
||||||
|
Format string `json:"format"`
|
||||||
NegativePrompt string `json:"negative_prompt"`
|
NegativePrompt string `json:"negative_prompt"`
|
||||||
NumInferenceSteps int `json:"num_inference_steps"` // Number of inference steps (minimum: 1; maximum: 500)
|
NumInferenceSteps int `json:"num_inference_steps"` // Number of inference steps (minimum: 1; maximum: 500)
|
||||||
GuidanceScale float32 `json:"guidance_scale"` // Scale for classifier-free guidance (minimum: 1; maximum: 20)
|
GuidanceScale float32 `json:"guidance_scale"` // Scale for classifier-free guidance (minimum: 1; maximum: 20)
|
||||||
@@ -144,8 +149,15 @@ type Image struct {
|
|||||||
- [x] GET [/api/images](/api/images) 圖片列表(全部)
|
- [x] GET [/api/images](/api/images) 圖片列表(全部)
|
||||||
- [x] GET [/api/images?user_id=xxx](/api/images?user_id=xxx) 圖片列表(指定用戶的)
|
- [x] GET [/api/images?user_id=xxx](/api/images?user_id=xxx) 圖片列表(指定用戶的)
|
||||||
- [x] GET [/api/images?tag=xxx](/api/images?tag=xxx) 圖片列表(指定標籤的)
|
- [x] GET [/api/images?tag=xxx](/api/images?tag=xxx) 圖片列表(指定標籤的)
|
||||||
|
- [x] POST [/api/images](/api/images) 上傳圖片(單張)
|
||||||
- [x] DELETE [/api/images/{id}](/api/images/{id}) 刪除指定圖像
|
- [x] DELETE [/api/images/{id}](/api/images/{id}) 刪除指定圖像
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 上傳圖片示例
|
||||||
|
response=$(curl -X POST -H "Content-Type: multipart/form-data" -F "file=@./data/test.jpeg" -b "session_id=$session_id" -s -w "%{http_code}" http://localhost:8080/api/images)
|
||||||
|
message "$response" "上傳圖片" true
|
||||||
|
```
|
||||||
|
|
||||||
任務:
|
任務:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
3
go.sum
3
go.sum
@@ -29,8 +29,6 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
|
|||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||||
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ=
|
|
||||||
golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
|
|
||||||
golang.org/x/image v0.7.0 h1:gzS29xtG1J5ybQlv0PuyfE3nmc6R4qB73m6LUUmvFuw=
|
golang.org/x/image v0.7.0 h1:gzS29xtG1J5ybQlv0PuyfE3nmc6R4qB73m6LUUmvFuw=
|
||||||
golang.org/x/image v0.7.0/go.mod h1:nd/q4ef1AKKYl/4kft7g+6UyGbdiqWqTP1ZAbRoV7Rg=
|
golang.org/x/image v0.7.0/go.mod h1:nd/q4ef1AKKYl/4kft7g+6UyGbdiqWqTP1ZAbRoV7Rg=
|
||||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
@@ -53,7 +51,6 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX
|
|||||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||||
|
|||||||
@@ -39,69 +39,72 @@ func ImagesGet(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ImagesPost(w http.ResponseWriter, r *http.Request) {
|
func ImagesPost(w http.ResponseWriter, r *http.Request) {
|
||||||
// 接收上傳的圖片文件, 僅限一張
|
models.AccountRead(w, r, func(account *models.Account) {
|
||||||
file, file_header, err := r.FormFile("file")
|
// 接收上傳的圖片文件, 僅限一張
|
||||||
if err != nil {
|
file, file_header, err := r.FormFile("file")
|
||||||
log.Println(err)
|
if err != nil {
|
||||||
return
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
// 圖片寬高
|
|
||||||
imgData, format, err := image.Decode(file)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(format, imgData.Bounds().Dx(), imgData.Bounds().Dy())
|
|
||||||
|
|
||||||
// 將文件指針移回開頭
|
|
||||||
if _, err := file.Seek(0, 0); err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 读取文件内容
|
|
||||||
content, err := ioutil.ReadAll(file)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 整理文件信息
|
|
||||||
var img models.Image
|
|
||||||
img.Name = file_header.Filename
|
|
||||||
img.Size = int(file_header.Size) // 數據大小
|
|
||||||
img.Hash = fmt.Sprintf("%x", md5.Sum(content)) // 计算哈希
|
|
||||||
img.Type = file_header.Header.Get("Content-Type") // 文件類型
|
|
||||||
img.Path = fmt.Sprintf("data/images/%s.%s", img.Hash, format) // 存儲路徑
|
|
||||||
img.Width = imgData.Bounds().Dx()
|
|
||||||
img.Height = imgData.Bounds().Dy()
|
|
||||||
img.Format = format
|
|
||||||
|
|
||||||
// 先檢查 data/images 目錄是否存在
|
|
||||||
if _, err := ioutil.ReadDir("data/images"); err != nil {
|
|
||||||
if err := os.Mkdir("data/images", 0777); err != nil {
|
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
defer file.Close()
|
||||||
|
|
||||||
// 將文件存儲到本地 data/images 目錄下
|
// 圖片寬高
|
||||||
if err := ioutil.WriteFile(img.Path, content, 0666); err != nil {
|
imgData, format, err := image.Decode(file)
|
||||||
log.Println(err)
|
if err != nil {
|
||||||
return
|
log.Println(err)
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 存儲圖片信息到數據庫
|
fmt.Println(format, imgData.Bounds().Dx(), imgData.Bounds().Dy())
|
||||||
if err := configs.ORMDB().Create(&img).Error; err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
// 將文件指針移回開頭
|
||||||
w.Write(utils.ToJSON(img))
|
if _, err := file.Seek(0, 0); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取文件内容
|
||||||
|
content, err := ioutil.ReadAll(file)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 整理文件信息
|
||||||
|
var img models.Image
|
||||||
|
img.Name = file_header.Filename
|
||||||
|
img.Size = int(file_header.Size) // 數據大小
|
||||||
|
img.Hash = fmt.Sprintf("%x", md5.Sum(content)) // 计算哈希
|
||||||
|
img.Type = file_header.Header.Get("Content-Type") // 文件類型
|
||||||
|
img.Path = fmt.Sprintf("data/images/%s.%s", img.Hash, format) // 存儲路徑
|
||||||
|
img.Width = imgData.Bounds().Dx() // 圖片寬度
|
||||||
|
img.Height = imgData.Bounds().Dy() // 圖片高度
|
||||||
|
img.Format = format // 圖片格式
|
||||||
|
img.UserID = account.ID // 用戶ID
|
||||||
|
|
||||||
|
// 先檢查 data/images 目錄是否存在
|
||||||
|
if _, err := ioutil.ReadDir("data/images"); err != nil {
|
||||||
|
if err := os.Mkdir("data/images", 0777); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 將文件存儲到本地 data/images 目錄下
|
||||||
|
if err := ioutil.WriteFile(img.Path, content, 0666); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 存儲圖片信息到數據庫
|
||||||
|
if err := configs.ORMDB().Create(&img).Error; err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
w.Write(utils.ToJSON(img))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func ImagesItemGet(w http.ResponseWriter, r *http.Request) {
|
func ImagesItemGet(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
Reference in New Issue
Block a user