119 lines
2.8 KiB
Go
119 lines
2.8 KiB
Go
package dbop
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/shockliu/logger"
|
|
"github.com/go-ini/ini"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
)
|
|
|
|
const (
|
|
imgurl = "https://center.daymele.com/res/images"
|
|
mp4url = "https://center.daymele.com/res/mp4"
|
|
apkurl = "https://center.daymele.com/res/apk"
|
|
)
|
|
|
|
var (
|
|
DB *sql.DB
|
|
err error // 错误信息
|
|
)
|
|
|
|
// 初始化链接
|
|
type Database struct {
|
|
Host string
|
|
Port string
|
|
Username string
|
|
Password string
|
|
Dbname string
|
|
}
|
|
|
|
type Redis struct {
|
|
Host string
|
|
Port string
|
|
Password string
|
|
}
|
|
|
|
// 初始化链接
|
|
func DBinit(conf string) {
|
|
Cfg, err := ini.Load(conf)
|
|
if err != nil {
|
|
panic("配置文件读取失败")
|
|
}
|
|
|
|
var my = &Database{}
|
|
err = Cfg.Section("mysql").MapTo(my)
|
|
if err != nil {
|
|
panic("未配置mysql数据库")
|
|
}
|
|
|
|
// 豆曲咖数据库
|
|
dbDSN := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s", my.Username, my.Password, my.Host, my.Port, my.Dbname, "utf8mb4")
|
|
// 打开连接失败
|
|
DB, err = sql.Open("mysql", dbDSN)
|
|
//defer MysqlDb.Close();
|
|
if err != nil {
|
|
logger.Error("dbDSN: " + dbDSN)
|
|
panic("数据源配置不正确: " + err.Error())
|
|
}
|
|
|
|
// 最大连接数
|
|
DB.SetMaxOpenConns(10)
|
|
// 闲置连接数
|
|
DB.SetMaxIdleConns(5)
|
|
// 最大连接周期
|
|
DB.SetConnMaxLifetime(100 * time.Second)
|
|
if err = DB.Ping(); nil != err {
|
|
panic("数据库链接失败: " + err.Error())
|
|
}
|
|
|
|
var rd = &Redis{}
|
|
err = Cfg.Section("redis").MapTo(rd)
|
|
if err != nil {
|
|
panic("未配置redis数据库")
|
|
}
|
|
RedisInit(rd.Host, rd.Port, rd.Password)
|
|
}
|
|
|
|
func InsertRecord(file, original, ftype, md5 string, size int64) (ret string) {
|
|
// time.Now().Format("2006-01/")
|
|
// https://www.52shiji.com/res/images/
|
|
if ftype == "movie/mp4" {
|
|
ret = fmt.Sprintf("%s/%s/%s", mp4url, time.Now().Format("2006-01"), file)
|
|
} else if ftype == "apk" {
|
|
ret = fmt.Sprintf("%s/%s", apkurl, file)
|
|
} else {
|
|
ret = fmt.Sprintf("%s/%s/%s", imgurl, time.Now().Format("2006-01"), file)
|
|
}
|
|
_, err := DB.Exec("INSERT INTO dqk_storage (`key`, name, type,md5,size,url,add_time) VALUES (?, ?,?,?,?,?,now());", file, original, ftype, md5, size, ret)
|
|
if err != nil {
|
|
logger.Errorf("文件[%s]入库失败:%s\n", file, err)
|
|
} else {
|
|
logger.Infof("文件入库[%s]成功。\n", file)
|
|
}
|
|
return
|
|
}
|
|
|
|
func IsExist(name string) bool {
|
|
var cnt int
|
|
s := fmt.Sprintf("SELECT count(1) FROM dqk_storage where `key` like '%s%%';", name)
|
|
//logger.Debugf("SQL:[%s]\n",s)
|
|
err := DB.QueryRow(s).Scan(&cnt)
|
|
if err != nil {
|
|
logger.Errorf("获取资源[%s]信息失败:%s\n", name, err)
|
|
return true
|
|
}
|
|
if cnt > 0 {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
func GetFilebyMd5(filename, md5 string, filesize int64) (name, url string, err error) {
|
|
err = DB.QueryRow("SELECT name,url FROM dqk_storage where md5=? and size=?;", md5, filesize).Scan(&name, &url)
|
|
return
|
|
}
|