mirror of https://github.com/MedzikUser/go-cdn
Compare commits
16 Commits
Author | SHA1 | Date |
---|---|---|
dependabot[bot] | c46cac3be8 | |
dependabot[bot] | 277af280f1 | |
MedzikUser | f67a0c0e8c | |
MedzikUser | ccab8d6482 | |
MedzikUser | 6080d3274c | |
dependabot[bot] | 7123ce9c84 | |
dependabot[bot] | 261335f905 | |
dependabot[bot] | 1028b74953 | |
MedzikUser | 63811dc8ee | |
dependabot[bot] | fede3de7a4 | |
MedzikUser | 5c7f6c4b87 | |
dependabot[bot] | 4c9c456ed3 | |
Medzik | 36ea49e9ac | |
Medzik | 87c7dc4f8f | |
Medzik | b30d28ac0e | |
Medzik | 87933a3f92 |
|
@ -12,13 +12,9 @@
|
|||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
# Output
|
||||
*.out
|
||||
cdn*
|
||||
|
||||
# Sums
|
||||
MD5SUM
|
||||
SHA256SUM
|
||||
|
||||
VERSION
|
||||
dist/
|
||||
|
||||
# Config
|
||||
config.yml
|
||||
|
|
5
Makefile
5
Makefile
|
@ -6,9 +6,6 @@ build:
|
|||
run:
|
||||
go run .
|
||||
|
||||
deps:
|
||||
go get -v
|
||||
|
||||
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
|
||||
|
||||
![Pipeline Badge](https://gitlab.com/gaming0skar123/go/cdn/badges/main/pipeline.svg)
|
||||
|
||||
## Install Pre-Compile binary
|
||||
|
||||
* Linux
|
||||
* Downoad `curl -L https://gitlab.com/gaming0skar123/go/cdn/-/jobs/artifacts/main/raw/cdn-linux-amd64?job=build --output cdn`
|
||||
* Add permissions `chmod +rwx cdn`
|
||||
* Run binary `./cdn`
|
||||
* Linux amd64
|
||||
* [Download](https://github.com/MedzikUser/go-cdn/releases) latest version
|
||||
* Unpack file `tar xzf cdn_{VERSION}_linux_amd64.tar.gz`
|
||||
* 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
|
||||
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
discord:
|
||||
api_channel: 234567890123456789
|
||||
channels:
|
||||
- 123456789012345678
|
||||
- 987654321098765433
|
|
@ -4,5 +4,5 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
var GH_Repo = "MagicJuszer/go-cdn"
|
||||
var GH_Repo = "MedzikUser/go-cdn"
|
||||
var Latest_Version_Check = 2 * time.Minute
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
package config
|
||||
|
||||
var Version = "1.0.0"
|
||||
var Version = "1.1.1"
|
||||
|
|
|
@ -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"
|
||||
"gitlab.com/gaming0skar123/go/cdn/common"
|
||||
"gitlab.com/gaming0skar123/go/cdn/config"
|
||||
"gitlab.com/gaming0skar123/go/cdn/database"
|
||||
)
|
||||
|
||||
var c *disgord.Client
|
||||
|
@ -17,17 +16,12 @@ func apiInit(client *disgord.Client) {
|
|||
}
|
||||
|
||||
func API(link string) {
|
||||
channel, err := database.FindAPIChannel()
|
||||
if common.CheckErr(err, "FindAPIChannel()") {
|
||||
s, err := snowflake.GetSnowflake(config.API_Channel)
|
||||
if common.CheckErr(err, "get snowflake") {
|
||||
return
|
||||
}
|
||||
|
||||
s, err := snowflake.GetSnowflake(channel.ID)
|
||||
if common.CheckErr(err, "GetSnowflake("+channel.ID+")") {
|
||||
return
|
||||
}
|
||||
|
||||
c.SendMsg(s, disgord.Embed{
|
||||
_, err = c.SendMsg(s, disgord.Embed{
|
||||
Title: link,
|
||||
Timestamp: disgord.Time{
|
||||
Time: time.Now().UTC(),
|
||||
|
@ -37,4 +31,8 @@ func API(link string) {
|
|||
URL: link,
|
||||
},
|
||||
})
|
||||
|
||||
if common.CheckErr(err, "send msg") {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,7 +29,10 @@ func Bot() {
|
|||
// DMIntents: disgord.IntentDirectMessages | disgord.IntentDirectMessageReactions | disgord.IntentDirectMessageTyping,
|
||||
})
|
||||
|
||||
defer client.Gateway().StayConnectedUntilInterrupted()
|
||||
defer func() {
|
||||
err := client.Gateway().StayConnectedUntilInterrupted()
|
||||
common.CheckErr(err, "stay bot connection")
|
||||
}()
|
||||
|
||||
filter, _ := std.NewMsgFilter(ctx, client)
|
||||
|
||||
|
|
|
@ -11,6 +11,6 @@ func deleteMsg(s disgord.Session, m *disgord.Message, t time.Duration) {
|
|||
go func() {
|
||||
time.Sleep(t)
|
||||
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"
|
||||
"gitlab.com/gaming0skar123/go/cdn/common"
|
||||
"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) {
|
||||
m := data.Message
|
||||
|
||||
findVaue := &database.Channel{
|
||||
ID: m.ChannelID.String(),
|
||||
GuildID: m.GuildID.String(),
|
||||
}
|
||||
|
||||
if !database.FindChannel(findVaue) {
|
||||
if !findChannel(m.ChannelID.String()) {
|
||||
return
|
||||
}
|
||||
|
||||
err := s.Channel(m.ChannelID).TriggerTypingIndicator()
|
||||
common.CheckErr(err, "TriggerTypingIndicator()")
|
||||
|
||||
avatarUrl, _ := m.Author.AvatarURL(64, true)
|
||||
|
||||
var url string
|
||||
common.CheckErr(err, "trigger typing")
|
||||
|
||||
if len(m.Attachments) > 0 {
|
||||
url = m.Attachments[0].URL
|
||||
} else {
|
||||
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)
|
||||
for _, a := range m.Attachments {
|
||||
uploadImg(s, m, a.URL)
|
||||
}
|
||||
|
||||
deleteMsg(s, m, 3*time.Second)
|
||||
|
||||
return
|
||||
} else {
|
||||
uploadImg(s, m, m.Content)
|
||||
}
|
||||
|
||||
_, 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)
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
8
go.mod
8
go.mod
|
@ -3,10 +3,10 @@ module gitlab.com/gaming0skar123/go/cdn
|
|||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/andersfylling/disgord v0.27.2
|
||||
github.com/andersfylling/disgord v0.29.0
|
||||
github.com/andersfylling/snowflake/v5 v5.0.1
|
||||
github.com/blang/semver v3.5.1+incompatible
|
||||
github.com/gin-gonic/gin v1.7.2
|
||||
github.com/gin-gonic/gin v1.7.4
|
||||
github.com/go-playground/validator/v10 v10.7.0 // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
|
@ -27,11 +27,11 @@ require (
|
|||
github.com/ulikunitz/xz v0.5.10 // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
|
||||
gitlab.com/gaming0skar123/go/modules/imgur v0.0.1-alpha.3
|
||||
go.mongodb.org/mongo-driver v1.6.0
|
||||
go.mongodb.org/mongo-driver v1.7.2
|
||||
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
|
||||
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914 // indirect
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
|
||||
google.golang.org/appengine v1.6.7 // indirect
|
||||
google.golang.org/protobuf v1.27.1 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
)
|
||||
|
|
12
go.sum
12
go.sum
|
@ -33,8 +33,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
|
|||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/andersfylling/disgord v0.27.2 h1:0RJ01Fs33gyf3Lny4q6ORxbYvYM6HyD1b6d19XQ6sLs=
|
||||
github.com/andersfylling/disgord v0.27.2/go.mod h1:xVShFExKzAKn/jtFdGPanouNsJ8FEe02vo+2nv78YRc=
|
||||
github.com/andersfylling/disgord v0.29.0 h1:nAFeDGaiLiUd4hj2ghOdVTYAJ9nLV5acNNBa+DCl5sI=
|
||||
github.com/andersfylling/disgord v0.29.0/go.mod h1:xVShFExKzAKn/jtFdGPanouNsJ8FEe02vo+2nv78YRc=
|
||||
github.com/andersfylling/snowflake/v5 v5.0.1 h1:unXbYSij6tRCGJzoLz9zl3nJsqd9hu7bbYSgB8K8/i0=
|
||||
github.com/andersfylling/snowflake/v5 v5.0.1/go.mod h1:AdhrB+kewjnQInv8cR7ABe2SGoVXh79njnipUnz1HFc=
|
||||
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
|
||||
|
@ -56,8 +56,8 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
|
|||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
|
||||
github.com/gin-gonic/gin v1.7.2 h1:Tg03T9yM2xa8j6I3Z3oqLaQRSmKvxPd6g/2HJ6zICFA=
|
||||
github.com/gin-gonic/gin v1.7.2/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
|
||||
github.com/gin-gonic/gin v1.7.4 h1:QmUZXrvJ9qZ3GfWvQ+2wnW/1ePrTEJqPKMYEU3lD/DM=
|
||||
github.com/gin-gonic/gin v1.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
|
||||
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||
|
@ -282,8 +282,8 @@ github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
|
|||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
gitlab.com/gaming0skar123/go/modules/imgur v0.0.1-alpha.3 h1:UKH8ZURQNfyav+cMpyjrf/WPkb7sLJiPUnezKlmQrX4=
|
||||
gitlab.com/gaming0skar123/go/modules/imgur v0.0.1-alpha.3/go.mod h1:Y8l7x4FzTVYVgQXCZJOMs8H7eDzlEyxhTBhW6FEr5Bo=
|
||||
go.mongodb.org/mongo-driver v1.6.0 h1:ccc26ylcoRWJQRbjU7GvqfxNzwKcoIcEL3BPuFR/pJ0=
|
||||
go.mongodb.org/mongo-driver v1.6.0/go.mod h1:Q4oFMbo1+MSNqICAdYMlC/zSTrwCogR4R8NzkI+yfU8=
|
||||
go.mongodb.org/mongo-driver v1.7.2 h1:pFttQyIiJUHEn50YfZgC9ECjITMT44oiN36uArf/OFg=
|
||||
go.mongodb.org/mongo-driver v1.7.2/go.mod h1:Q4oFMbo1+MSNqICAdYMlC/zSTrwCogR4R8NzkI+yfU8=
|
||||
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
|
||||
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
|
||||
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
|
||||
|
|
|
@ -15,7 +15,7 @@ func Update() {
|
|||
repo := config.GH_Repo
|
||||
|
||||
latest, found, err := selfupdate.DetectLatest(repo)
|
||||
if common.CheckErr(err, "detecting letest version") {
|
||||
if common.CheckErr(err, "detect latest version") {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -23,13 +23,13 @@ func Upload(c *gin.Context) {
|
|||
}
|
||||
|
||||
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")
|
||||
return
|
||||
}
|
||||
|
||||
f, err := file.Open()
|
||||
if common.CheckErr(err, "error opening file") {
|
||||
if common.CheckErr(err, "open file") {
|
||||
c.String(http.StatusBadRequest, "Error Opening File")
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue