Skip to content

Commit

Permalink
Telegram notifications
Browse files Browse the repository at this point in the history
update deps, small refactoring
  • Loading branch information
sorokinmax committed Feb 28, 2024
1 parent 3deb46b commit 5ceb4c4
Show file tree
Hide file tree
Showing 213 changed files with 22,292 additions and 5,433 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
##
## Build stage
##
FROM golang:1.21.5-alpine3.19 AS build
FROM golang:1.21.7-alpine3.19 AS build
RUN apk update && apk upgrade&& \
apk add --no-cache git gcc g++ musl-dev
COPY . /src
Expand Down
30 changes: 16 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module android-store

go 1.21.5
go 1.21.7

require (
github.com/avast/apkparser v0.0.0-20231031113741-3ff9e55acf2f
Expand All @@ -12,41 +12,43 @@ require (
github.com/foolin/goview v0.3.0
github.com/gin-gonic/contrib v0.0.0-20221130124618-7e01895a63f2
github.com/gin-gonic/gin v1.9.1
gorm.io/driver/sqlite v1.5.4
gorm.io/gorm v1.25.5
gopkg.in/telebot.v3 v3.2.1
gorm.io/driver/sqlite v1.5.5
gorm.io/gorm v1.25.7
)

require (
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/bytedance/sonic v1.11.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.16.0 // indirect
github.com/go-playground/validator/v10 v10.18.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/knz/go-libedit v1.10.1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.19 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.6.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/image v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/image v0.15.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
851 changes: 848 additions & 3 deletions go.sum

Large diffs are not rendered by default.

19 changes: 11 additions & 8 deletions internal/api/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"android-store/internal/db"
"android-store/internal/globals"
models "android-store/internal/models/apk"
telegram "android-store/pkg/telegram"
"fmt"
"image/png"
"log"
Expand All @@ -22,8 +23,8 @@ import (
func IndexHandler(ctx *gin.Context) {
var apks []models.Apk
apks, _ = db.SQLiteGetApks()
for id, apk := range apks {
apks[id].URL = fmt.Sprintf("%s/apk/%s/%s", globals.Config.Url, apk.SHA256, apk.FileName)
for id, app := range apks {
apks[id].URL = apk.GetAPKURL(app)
}

e := casbin.NewEnforcer("./data/model.conf", "./data/policy.csv")
Expand Down Expand Up @@ -83,14 +84,14 @@ func VersionHandler(ctx *gin.Context) {
}

func QRHandler(ctx *gin.Context) {
var apk models.Apk
var app models.Apk
var id = ctx.Param("id")

apk, _ = db.SQLiteGetApk(id)
apk.URL = fmt.Sprintf("%s/apps/%s/%s", globals.Config.Url, apk.SHA256, apk.FileName)
app, _ = db.SQLiteGetApk(id)
app.URL = apk.GetAPKURL(app)

data := apk.URL
description := fmt.Sprintf("%s-%s (%s)", apk.AppLabel, apk.VersionName, apk.Package)
data := app.URL
description := fmt.Sprintf("%s-%s (%s)", app.AppLabel, app.VersionName, app.Package)

qrCode, _ := qr.Encode(data, qr.L, qr.Auto)
qrCode, _ = barcode.Scale(qrCode, 600, 600)
Expand Down Expand Up @@ -132,7 +133,9 @@ func PostApkHandler(ctx *gin.Context) {
log.Fatal(err)
}

apk.ApkProcessor("./apps", file.Filename)
app := apk.ApkProcessor("./apps", file.Filename)

msg := fmt.Sprintf("New build %s %s is ready", app.AppLabel, app.VersionName)
telegram.TgSendMessage(globals.Config.BotToken, msg, globals.Config.ChatID)
ctx.JSON(http.StatusOK, gin.H{"responce": "File processed"})
}
14 changes: 12 additions & 2 deletions internal/apk/apk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package apk

import (
"android-store/internal/db"
"android-store/internal/globals"
models "android-store/internal/models/apk"
"bytes"
"crypto/sha256"
Expand All @@ -16,7 +17,7 @@ import (
"github.com/avast/apkparser"
)

func ApkProcessor(dirPath string, fileName string) {
func ApkProcessor(dirPath string, fileName string) *models.Apk {
var apks []models.Apk
var apk models.Apk

Expand All @@ -25,7 +26,7 @@ func ApkProcessor(dirPath string, fileName string) {
manifest, err := apkParse(filePath)
if err != nil {
//fmt.Fprintf(os.Stderr, "Failed to open the APK: %s", zipErr.Error())
return
return nil
}

apk.Package = manifest.Package
Expand All @@ -40,6 +41,9 @@ func ApkProcessor(dirPath string, fileName string) {
apks, _ = db.SQLiteGetApks()
if !containsApks(apks, apk) {
db.SQLiteAddApk(&apk)
} else {
log.Printf("%s already exist", apk.VersionName)
return nil
}

/*if err == nil {
Expand All @@ -65,6 +69,7 @@ func ApkProcessor(dirPath string, fileName string) {
panic(err)
}
moveFile(filePath, fmt.Sprintf("./apps/%s/%s", apk.SHA256, fileName))
return &apk
}

func apkParse(name string) (*models.Manifest, error) {
Expand Down Expand Up @@ -129,3 +134,8 @@ func moveFile(source string, destination string) {
fmt.Println(err)
}
}

func GetAPKURL(apk models.Apk) (URL string) {
URL = fmt.Sprintf("%s/apk/%s/%s", globals.Config.Url, apk.SHA256, apk.FileName)
return URL
}
2 changes: 2 additions & 0 deletions internal/globals/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const Version = "v.1.0.0"
type ConfigStruct struct {
HttpPort uint `env:"AS_HTTP_PORT" envDefault:"80"`
Url string `env:"AS_HTTP_PORT" envDefault:"http://localhost:80"`
BotToken string `env:"TELEGRAM_BOT_TOKEN"`
ChatID int `env:"TELEGRAM_CHAT_ID"`
}

// GLOBAL VARIABLES
Expand Down
28 changes: 28 additions & 0 deletions pkg/telegram/telegram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package telegram

import (
"log"

tb "gopkg.in/telebot.v3"
)

func TgSendMessage(botToken string, msg string, chatID int) (responce *tb.Message) {
log.Printf("Sending to chat: %d", chatID)

tbot, err := tb.NewBot(tb.Settings{
Token: botToken,
})
if err != nil {
log.Println(err)
} else {
group := tb.ChatID(chatID)
var opts tb.SendOptions
opts.ParseMode = tb.ModeHTML
responce, err = tbot.Send(group, msg, &opts)
if err != nil {
log.Println(err)
log.Println(msg)
}
}
return responce
}
Loading

0 comments on commit 5ceb4c4

Please sign in to comment.