Skip to content

Commit

Permalink
upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
gocloudcoder committed Apr 20, 2021
1 parent 2477b44 commit bc26b37
Show file tree
Hide file tree
Showing 31 changed files with 608 additions and 147 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,7 @@ qn put -w test1.png test2.png
- [x] 支持通配符(批量传入)

- [ ] 改进路径问题(去除路径中的空格)

## 更新

* 4.20 可将网络上的文件上传到七牛中
75 changes: 75 additions & 0 deletions cmd/fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"fmt"
"github.com/qiniu/api.v7/v7/auth/qbox"
"github.com/qiniu/api.v7/v7/storage"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
)

// fetchCmd represents the fetch command
var fetchCmd = &cobra.Command{
Use: "fetch",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: fetch,
}

func fetch(cmd *cobra.Command, params []string) {
remoteUrl := params[0]
accessKey := viper.GetString("ak")
secretKey := viper.GetString("sk")
bucket := viper.GetString("bucket")
if accessKey == "" || secretKey == "" || bucket == "" {
fmt.Println("请设置ak, sk, bucket")
os.Exit(0)
}

mac := qbox.NewMac(accessKey, secretKey)
cfg := storage.Config{
ApiHost:"http://api.qiniu.com",
}
bm := storage.NewBucketManager(mac, &cfg)
res, err := bm.Fetch(remoteUrl, bucket, "test123456.png")
if err != nil {
fmt.Println(err)
}
fmt.Println(res.Key)

}

func init() {
rootCmd.AddCommand(fetchCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// fetchCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// fetchCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
121 changes: 77 additions & 44 deletions cmd/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type PutRet struct {

// putCmd represents the put command
var putCmd = &cobra.Command{
Use: "put <local file> [<local file>] [flags]",
Use: "put <local/remote file> [<local/remote file>] [flags]",
Short: "put file to qiniu server",
Long: `use put command, you can put your local file to qiniu server, and get a url.`,
Run: put,
Expand All @@ -66,59 +66,92 @@ func put(cmd *cobra.Command, params []string){
}

for _, path := range params {
index := strings.LastIndexAny(path, "/")
var upload string
if index != -1 {
upload = path[index+1:]
if isRemoteFile(path) {
putRemoteFile(path, mac, bucket, cfg)
} else {
upload = path
putLocalFile(path, mac, bucket, cfg)
}
}
}

var putPolicy storage.PutPolicy

if overwrite {
if path == upload {
err := os.Chdir(".")
if err != nil {
fmt.Println("err")
}
} else {
err := os.Chdir(path[0:index+1])
path = upload
if err != nil {
fmt.Println("err")
}
}
putPolicy = storage.PutPolicy{
Scope: fmt.Sprintf("%s:%s", bucket, path),

func isRemoteFile(path string) bool {
return strings.HasPrefix(path, "http")
}

func getFinalKey(path string) string {
var finalKey string
index := strings.LastIndex(path, "/")
if index != -1 {
finalKey = path[index+1:]
}
return finalKey
}

func putRemoteFile(path string, mac *qbox.Mac, bucket string, cfg storage.Config) {
finalKey := getFinalKey(path)
bm := storage.NewBucketManager(mac, &cfg)
res, err := bm.Fetch(path, bucket, finalKey)
if err != nil {
fmt.Println(err)
}
domains, _ := bm.ListBucketDomains(bucket)
fmt.Println("http://" + domains[0].Domain + "/" + res.Key)
}

func putLocalFile(path string, mac *qbox.Mac, bucket string, cfg storage.Config) {
index := strings.LastIndexAny(path, "/")
var upload string
if index != -1 {
upload = path[index+1:]
} else {
upload = path
}

var putPolicy storage.PutPolicy

if overwrite {
if path == upload {
err := os.Chdir(".")
if err != nil {
fmt.Println("err")
}
} else {
putPolicy = storage.PutPolicy{
Scope: bucket,
err := os.Chdir(path[0:index+1])
path = upload
if err != nil {
fmt.Println("err")
}

}
upToken := putPolicy.UploadToken(mac)
bm := storage.NewBucketManager(mac, &cfg)
domains, err := bm.ListBucketDomains(bucket)
if err != nil {
fmt.Println("get domain err")
putPolicy = storage.PutPolicy{
Scope: fmt.Sprintf("%s:%s", bucket, path),
}
formUploader := storage.NewFormUploader(&cfg)
ret := storage.PutPolicy{}
putExtra := storage.PutExtra{
Params: map[string]string{
"x:name": "picture or some other data",
},
} else {
putPolicy = storage.PutPolicy{
Scope: bucket,
}
err = formUploader.PutFile(context.Background(), &ret, upToken, upload, path, &putExtra)
if err != nil {
fmt.Println(err)
fmt.Println("上传失败")
return
}
fmt.Println("http://" + domains[0].Domain + "/" + upload)

}
upToken := putPolicy.UploadToken(mac)
bm := storage.NewBucketManager(mac, &cfg)
domains, err := bm.ListBucketDomains(bucket)
if err != nil {
fmt.Println("get domain err")
}
formUploader := storage.NewFormUploader(&cfg)
ret := storage.PutPolicy{}
putExtra := storage.PutExtra{
Params: map[string]string{
"x:name": "picture or some other data",
},
}
err = formUploader.PutFile(context.Background(), &ret, upToken, upload, path, &putExtra)
if err != nil {
fmt.Println(err)
fmt.Println("上传失败")
return
}
fmt.Println("http://" + domains[0].Domain + "/" + upload)
}

func init() {
Expand Down
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ var rootCmd = &cobra.Command{
Use: "qn",
Short: "simple qiniu-cli",
Long: `qiniu-cli is based on qiniu product about go sdk, I just simplify it in order to use it easily`,

}

func Execute() {
Expand Down
Binary file added free-pic/cli/qn-cli.exe
Binary file not shown.
8 changes: 4 additions & 4 deletions free-pic/web/server/conf/config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
mysql:
ip: "localhost"
ip: "gocloudcoder.com"
port: "3306"
username: "qn-server"
database: "qn-server"
password: "000000"

qiniu:
ak: ""
sk: ""
bucket: ""
ak: "zM_pCg7E1kBvwD0DlGQUadtxVGkumuKNQIDVI4Nl"
sk: "P2mdTgHaJ7PPX7cggzpBHti-VKppSh-Mqkj-hWeO"
bucket: "pic-gocloudcoder"
2 changes: 1 addition & 1 deletion free-pic/web/server/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func UpToken(c *gin.Context) {
token := service.GenerateToken()
c.IndentedJSON(200, gin.H{
c.JSON(200, gin.H{
"token":token,
})
}
Expand Down
59 changes: 59 additions & 0 deletions free-pic/web/server/controllers/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package controllers

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"qn/server/models"
"qn/server/service"
)

func QueryAllLogin(c *gin.Context) {
allLogin := service.QueryLogin()
c.IndentedJSON(200, allLogin)
}

// @Summary 登录接口
// @Produce json
// @Param username query string true "登录名"
// @Param password query string true "密码"
// @Header
// @Router /api/v2/trueLogin [post]
func TrueLogin(c *gin.Context) {
username := c.PostForm("username")
password := c.PostForm("password")
fmt.Println(username)
fmt.Println(password)
tokenString, err := service.TrueLogin(username, password)
if err != nil {
c.IndentedJSON(200, models.NewLoginErr())
} else {
c.IndentedJSON(http.StatusOK, gin.H{
"code": 20000,
"token": tokenString,
})
}
}


// @Summary 注册接口
// @Produce json
// @Param username query string true "用户名"
// @Param password query string true "密码"
// @Success 200 {} json "{"code":200, "msg":"success add user"}"
// @Failure 401 {} json "{"code":401, "msg":"add user error"}"
// @Router /api/v2/login/add [post]
func PostLogin(c *gin.Context) {
username := c.PostForm("username")
password := c.PostForm("password")
status := service.PostLogin(username, password)
if status == 200 {
c.IndentedJSON(http.StatusOK, gin.H{
"code" : 200,
"message" : "success add user",
})
} else {
c.IndentedJSON(http.StatusOK, models.NewUserHasExistedErr(username))
}
}
4 changes: 3 additions & 1 deletion free-pic/web/server/global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package global

import (
"github.com/qiniu/api.v7/v7/auth/qbox"
"gorm.io/gorm"
)

var (
MAC *qbox.Mac
)
QN_DB *gorm.DB
)
6 changes: 4 additions & 2 deletions free-pic/web/server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ module qn/server
go 1.16

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gin-gonic/gin v1.6.3
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/qiniu/api.v7/v7 v7.8.1
github.com/spf13/viper v1.7.1
github.com/ugorji/go v1.2.4 // indirect
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
golang.org/x/sys v0.0.0-20210228012217-479acdf4ea46 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gorm.io/driver/mysql v1.0.5
gorm.io/gorm v1.21.6
)
Loading

0 comments on commit bc26b37

Please sign in to comment.