diff --git a/.gitignore b/.gitignore index ce1bc77..c26777d 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Makefile b/Makefile index 96fbf16..96f5f25 100644 --- a/Makefile +++ b/Makefile @@ -6,9 +6,6 @@ build: run: go run . -deps: - go get -v - clean: go clean - rm cdn* dist + rm ${OUT_FILE} dist diff --git a/README.md b/README.md index ef417a8..31a5ab2 100644 --- a/README.md +++ b/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 diff --git a/config.schema.yml b/config.schema.yml new file mode 100644 index 0000000..fefc63f --- /dev/null +++ b/config.schema.yml @@ -0,0 +1,5 @@ +discord: + api_channel: 234567890123456789 + channels: + - 123456789012345678 + - 987654321098765433 diff --git a/config/update.go b/config/update.go index 6a2e271..f6e1f67 100644 --- a/config/update.go +++ b/config/update.go @@ -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 diff --git a/config/version.go b/config/version.go index d2184b3..94a0e68 100644 --- a/config/version.go +++ b/config/version.go @@ -1,3 +1,3 @@ package config -var Version = "1.0.0" +var Version = "1.1.0" diff --git a/config/yml.go b/config/yml.go new file mode 100644 index 0000000..36f342a --- /dev/null +++ b/config/yml.go @@ -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 +} diff --git a/database/channel.go b/database/channel.go deleted file mode 100644 index 84bfd58..0000000 --- a/database/channel.go +++ /dev/null @@ -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 -} diff --git a/discord/api.go b/discord/api.go index e7b0f40..a0f57be 100644 --- a/discord/api.go +++ b/discord/api.go @@ -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,13 +16,8 @@ func apiInit(client *disgord.Client) { } func API(link string) { - channel, err := database.FindAPIChannel() - if common.CheckErr(err, "FindAPIChannel()") { - return - } - - s, err := snowflake.GetSnowflake(channel.ID) - if common.CheckErr(err, "GetSnowflake("+channel.ID+")") { + s, err := snowflake.GetSnowflake(config.API_Channel) + if common.CheckErr(err, "get snowflake") { return } diff --git a/discord/deleteMsg.go b/discord/deleteMsg.go index fd5388b..277c896 100644 --- a/discord/deleteMsg.go +++ b/discord/deleteMsg.go @@ -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") }() } diff --git a/discord/handleMsg.go b/discord/handleMsg.go index f6e01c2..6da25ae 100644 --- a/discord/handleMsg.go +++ b/discord/handleMsg.go @@ -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) } diff --git a/discord/uploadImg.go b/discord/uploadImg.go new file mode 100644 index 0000000..441d688 --- /dev/null +++ b/discord/uploadImg.go @@ -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") +} diff --git a/go.mod b/go.mod index 20ac00b..551d62f 100644 --- a/go.mod +++ b/go.mod @@ -33,5 +33,5 @@ require ( 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 ) diff --git a/update/update.go b/update/update.go index d0d3143..b01ea7c 100644 --- a/update/update.go +++ b/update/update.go @@ -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 } diff --git a/website/routers/upload.go b/website/routers/upload.go index 83d3c74..e52fd63 100644 --- a/website/routers/upload.go +++ b/website/routers/upload.go @@ -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 }