mirror of https://github.com/MedzikUser/go-cdn
Added YML config and More
- Discord Bot: Added YML config and moved bot settings from DB to this file - Error logs names has been renames - Discord Bot: Added upload from multi attachments - Update README, Makefile, go.mod and .gitignore
This commit is contained in:
parent
b30d28ac0e
commit
87c7dc4f8f
|
@ -12,13 +12,9 @@
|
||||||
# Test binary, built with `go test -c`
|
# Test binary, built with `go test -c`
|
||||||
*.test
|
*.test
|
||||||
|
|
||||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
# Output
|
||||||
*.out
|
*.out
|
||||||
cdn*
|
|
||||||
|
|
||||||
# Sums
|
|
||||||
MD5SUM
|
|
||||||
SHA256SUM
|
|
||||||
|
|
||||||
VERSION
|
|
||||||
dist/
|
dist/
|
||||||
|
|
||||||
|
# Config
|
||||||
|
config.yml
|
||||||
|
|
5
Makefile
5
Makefile
|
@ -6,9 +6,6 @@ build:
|
||||||
run:
|
run:
|
||||||
go run .
|
go run .
|
||||||
|
|
||||||
deps:
|
|
||||||
go get -v
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
go clean
|
go clean
|
||||||
rm cdn* dist
|
rm ${OUT_FILE} dist
|
||||||
|
|
12
README.md
12
README.md
|
@ -1,13 +1,13 @@
|
||||||
# Image CDN - API and Discord Bot
|
# Image CDN - API and Discord Bot
|
||||||
|
|
||||||
![Pipeline Badge](https://gitlab.com/gaming0skar123/go/cdn/badges/main/pipeline.svg)
|
|
||||||
|
|
||||||
## Install Pre-Compile binary
|
## Install Pre-Compile binary
|
||||||
|
|
||||||
* Linux
|
* Linux amd64
|
||||||
* Downoad `curl -L https://gitlab.com/gaming0skar123/go/cdn/-/jobs/artifacts/main/raw/cdn-linux-amd64?job=build --output cdn`
|
* [Download](https://github.com/MedzikUser/go-cdn/releases) latest version
|
||||||
* Add permissions `chmod +rwx cdn`
|
* Unpack file `tar xzf cdn_{VERSION}_linux_amd64.tar.gz`
|
||||||
* Run binary `./cdn`
|
* Create an .env and config.yml file and complete according to .env.schema and config.schema.yml
|
||||||
|
* Add permissions `chmod +rwx cdn.out`
|
||||||
|
* Run binary `./cdn.out`
|
||||||
|
|
||||||
## Disable Automatic Updates
|
## Disable Automatic Updates
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
discord:
|
||||||
|
api_channel: 234567890123456789
|
||||||
|
channels:
|
||||||
|
- 123456789012345678
|
||||||
|
- 987654321098765433
|
|
@ -4,5 +4,5 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var GH_Repo = "MagicJuszer/go-cdn"
|
var GH_Repo = "MedzikUser/go-cdn"
|
||||||
var Latest_Version_Check = 2 * time.Minute
|
var Latest_Version_Check = 2 * time.Minute
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
var Version = "1.0.0"
|
var Version = "1.1.0"
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
|
"gitlab.com/gaming0skar123/go/cdn/common"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type YmlConfig struct {
|
||||||
|
Discord YmlConfigDiscord
|
||||||
|
}
|
||||||
|
|
||||||
|
type YmlConfigDiscord struct {
|
||||||
|
API_Channel string
|
||||||
|
Channels []string
|
||||||
|
}
|
||||||
|
|
||||||
|
var API_Channel string
|
||||||
|
var Channels []string
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
f, err := ioutil.ReadFile("config.yml")
|
||||||
|
if common.CheckErr(err, "read config file") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var data YmlConfig
|
||||||
|
err = yaml.Unmarshal(f, &data)
|
||||||
|
if common.CheckErr(err, "unmarshal config file") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
API_Channel = data.Discord.API_Channel
|
||||||
|
Channels = data.Discord.Channels
|
||||||
|
}
|
|
@ -1,61 +0,0 @@
|
||||||
package database
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitlab.com/gaming0skar123/go/cdn/common"
|
|
||||||
"gitlab.com/gaming0skar123/go/cdn/config"
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Channel struct {
|
|
||||||
ID string `bson:"_id"`
|
|
||||||
GuildID string `bson:"gid"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ChannelAPI struct {
|
|
||||||
ID string `bson:"_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func InsertChannel(channel *Channel) {
|
|
||||||
collection := DB.Collection(config.Mongo_Collection_Channel)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
_, err := collection.InsertOne(ctx, channel)
|
|
||||||
common.CheckErr(err, "inserting to DB")
|
|
||||||
}
|
|
||||||
|
|
||||||
func InsertAPIChannel(channel *ChannelAPI) {
|
|
||||||
collection := DB.Collection(config.Mongo_Collection_Channel_API)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
_, err := collection.InsertOne(ctx, channel)
|
|
||||||
common.CheckErr(err, "inserting to DB")
|
|
||||||
}
|
|
||||||
|
|
||||||
func FindChannel(channel *Channel) bool {
|
|
||||||
collection := DB.Collection(config.Mongo_Collection_Channel)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
err := collection.FindOne(ctx, channel).Decode(&channel)
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func FindAPIChannel() (*ChannelAPI, error) {
|
|
||||||
collection := DB.Collection(config.Mongo_Collection_Channel_API)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
var channel *ChannelAPI
|
|
||||||
|
|
||||||
err := collection.FindOne(ctx, bson.D{}).Decode(&channel)
|
|
||||||
return channel, err
|
|
||||||
}
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"github.com/andersfylling/snowflake/v5"
|
"github.com/andersfylling/snowflake/v5"
|
||||||
"gitlab.com/gaming0skar123/go/cdn/common"
|
"gitlab.com/gaming0skar123/go/cdn/common"
|
||||||
"gitlab.com/gaming0skar123/go/cdn/config"
|
"gitlab.com/gaming0skar123/go/cdn/config"
|
||||||
"gitlab.com/gaming0skar123/go/cdn/database"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var c *disgord.Client
|
var c *disgord.Client
|
||||||
|
@ -17,13 +16,8 @@ func apiInit(client *disgord.Client) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func API(link string) {
|
func API(link string) {
|
||||||
channel, err := database.FindAPIChannel()
|
s, err := snowflake.GetSnowflake(config.API_Channel)
|
||||||
if common.CheckErr(err, "FindAPIChannel()") {
|
if common.CheckErr(err, "get snowflake") {
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
s, err := snowflake.GetSnowflake(channel.ID)
|
|
||||||
if common.CheckErr(err, "GetSnowflake("+channel.ID+")") {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,6 @@ func deleteMsg(s disgord.Session, m *disgord.Message, t time.Duration) {
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(t)
|
time.Sleep(t)
|
||||||
err := s.Channel(m.ChannelID).Message(m.ID).Delete()
|
err := s.Channel(m.ChannelID).Message(m.ID).Delete()
|
||||||
common.CheckErr(err, "delete message")
|
common.CheckErr(err, "delete msg")
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,69 +6,35 @@ import (
|
||||||
"github.com/andersfylling/disgord"
|
"github.com/andersfylling/disgord"
|
||||||
"gitlab.com/gaming0skar123/go/cdn/common"
|
"gitlab.com/gaming0skar123/go/cdn/common"
|
||||||
"gitlab.com/gaming0skar123/go/cdn/config"
|
"gitlab.com/gaming0skar123/go/cdn/config"
|
||||||
"gitlab.com/gaming0skar123/go/cdn/database"
|
|
||||||
"gitlab.com/gaming0skar123/go/cdn/imgur"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func findChannel(channel string) bool {
|
||||||
|
for _, c := range config.Channels {
|
||||||
|
if c == channel {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func handleMsg(s disgord.Session, data *disgord.MessageCreate) {
|
func handleMsg(s disgord.Session, data *disgord.MessageCreate) {
|
||||||
m := data.Message
|
m := data.Message
|
||||||
|
|
||||||
findVaue := &database.Channel{
|
if findChannel(m.ChannelID.String()) {
|
||||||
ID: m.ChannelID.String(),
|
|
||||||
GuildID: m.GuildID.String(),
|
|
||||||
}
|
|
||||||
|
|
||||||
if !database.FindChannel(findVaue) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err := s.Channel(m.ChannelID).TriggerTypingIndicator()
|
err := s.Channel(m.ChannelID).TriggerTypingIndicator()
|
||||||
common.CheckErr(err, "TriggerTypingIndicator()")
|
common.CheckErr(err, "trigger typing")
|
||||||
|
|
||||||
avatarUrl, _ := m.Author.AvatarURL(64, true)
|
|
||||||
|
|
||||||
var url string
|
|
||||||
|
|
||||||
if len(m.Attachments) > 0 {
|
if len(m.Attachments) > 0 {
|
||||||
url = m.Attachments[0].URL
|
for _, a := range m.Attachments {
|
||||||
} else {
|
uploadImg(s, m, a.URL)
|
||||||
url = m.Content
|
|
||||||
}
|
|
||||||
|
|
||||||
i, err := imgur.UploadFromURL(url)
|
|
||||||
if common.CheckErr(err, "UploadFromURL("+url+")") {
|
|
||||||
mBot, err := m.Reply(ctx, s, disgord.Embed{
|
|
||||||
Title: "Error",
|
|
||||||
Description: err.Error(),
|
|
||||||
Timestamp: m.Timestamp,
|
|
||||||
Color: config.Embed_Color,
|
|
||||||
Footer: &disgord.EmbedFooter{
|
|
||||||
Text: m.Author.Tag(),
|
|
||||||
IconURL: avatarUrl,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if !common.CheckErr(err, "send message") {
|
|
||||||
deleteMsg(s, mBot, 5*time.Second)
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
deleteMsg(s, m, 3*time.Second)
|
uploadImg(s, m, m.Content)
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = m.Reply(ctx, s, disgord.Embed{
|
|
||||||
Title: i.Link,
|
|
||||||
Timestamp: m.Timestamp,
|
|
||||||
Color: config.Embed_Color,
|
|
||||||
Footer: &disgord.EmbedFooter{
|
|
||||||
Text: m.Author.Tag(),
|
|
||||||
IconURL: avatarUrl,
|
|
||||||
},
|
|
||||||
Image: &disgord.EmbedImage{
|
|
||||||
URL: i.Link,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
common.CheckErr(err, "send message")
|
|
||||||
|
|
||||||
deleteMsg(s, m, 2*time.Second)
|
deleteMsg(s, m, 2*time.Second)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
package discord
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/andersfylling/disgord"
|
||||||
|
"gitlab.com/gaming0skar123/go/cdn/common"
|
||||||
|
"gitlab.com/gaming0skar123/go/cdn/config"
|
||||||
|
"gitlab.com/gaming0skar123/go/cdn/imgur"
|
||||||
|
)
|
||||||
|
|
||||||
|
func uploadImg(s disgord.Session, m *disgord.Message, url string) {
|
||||||
|
avatarUrl, _ := m.Author.AvatarURL(64, true)
|
||||||
|
|
||||||
|
i, err := imgur.UploadFromURL(url)
|
||||||
|
if common.CheckErr(err, "upload img") {
|
||||||
|
mBot, err := m.Reply(ctx, s, disgord.Embed{
|
||||||
|
Title: "Error",
|
||||||
|
Description: err.Error(),
|
||||||
|
Timestamp: m.Timestamp,
|
||||||
|
Color: config.Embed_Color,
|
||||||
|
Footer: &disgord.EmbedFooter{
|
||||||
|
Text: m.Author.Tag(),
|
||||||
|
IconURL: avatarUrl,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if !common.CheckErr(err, "send msg") {
|
||||||
|
deleteMsg(s, mBot, 5*time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteMsg(s, m, 3*time.Second)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = m.Reply(ctx, s, disgord.Embed{
|
||||||
|
Title: i.Link,
|
||||||
|
Timestamp: m.Timestamp,
|
||||||
|
Color: config.Embed_Color,
|
||||||
|
Footer: &disgord.EmbedFooter{
|
||||||
|
Text: m.Author.Tag(),
|
||||||
|
IconURL: avatarUrl,
|
||||||
|
},
|
||||||
|
Image: &disgord.EmbedImage{
|
||||||
|
URL: i.Link,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
common.CheckErr(err, "send msg")
|
||||||
|
}
|
2
go.mod
2
go.mod
|
@ -33,5 +33,5 @@ require (
|
||||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
|
||||||
google.golang.org/appengine v1.6.7 // indirect
|
google.golang.org/appengine v1.6.7 // indirect
|
||||||
google.golang.org/protobuf v1.27.1 // indirect
|
google.golang.org/protobuf v1.27.1 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0
|
||||||
)
|
)
|
||||||
|
|
|
@ -15,7 +15,7 @@ func Update() {
|
||||||
repo := config.GH_Repo
|
repo := config.GH_Repo
|
||||||
|
|
||||||
latest, found, err := selfupdate.DetectLatest(repo)
|
latest, found, err := selfupdate.DetectLatest(repo)
|
||||||
if common.CheckErr(err, "detecting letest version") {
|
if common.CheckErr(err, "detect latest version") {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,13 +23,13 @@ func Upload(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := c.FormFile("file")
|
file, err := c.FormFile("file")
|
||||||
if common.CheckErr(err, "error form file") {
|
if common.CheckErr(err, "form file") {
|
||||||
c.String(http.StatusBadRequest, "Error Form File")
|
c.String(http.StatusBadRequest, "Error Form File")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := file.Open()
|
f, err := file.Open()
|
||||||
if common.CheckErr(err, "error opening file") {
|
if common.CheckErr(err, "open file") {
|
||||||
c.String(http.StatusBadRequest, "Error Opening File")
|
c.String(http.StatusBadRequest, "Error Opening File")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue