finished: check update

This commit is contained in:
2024-07-23 16:32:49 +08:00
parent b7d64cba7b
commit e0dbd03397
16 changed files with 511 additions and 37 deletions

83
service/download.go Normal file
View File

@@ -0,0 +1,83 @@
package service
import (
"fmt"
"io"
"net/http"
"os"
"strconv"
)
// ProgressWriter is a custom writer to track progress and execute a callback
type ProgressWriter struct {
io.Writer
Total int64
Progress int64
Callback func(progress int64, total int64)
}
func (pw *ProgressWriter) Write(p []byte) (int, error) {
n, err := pw.Writer.Write(p)
if err != nil {
return n, err
}
pw.Progress += int64(n)
pw.Callback(pw.Progress, pw.Total)
return n, nil
}
func DownloadFile(url string, filePath string, overwrite bool, callback func(progress int64, total int64)) error {
// Check if the file exists
if _, err := os.Stat(filePath); err == nil {
if overwrite {
// Overwrite the file if it exists
if err := os.Remove(filePath); err != nil {
return err
}
} else {
return fmt.Errorf("file %s already exists", filePath)
}
}
// Create the file
out, err := os.Create(filePath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Check server response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
// Get the size
size, err := strconv.Atoi(resp.Header.Get("Content-Length"))
if err != nil {
return err
}
// Create progress writer
pw := &ProgressWriter{
Writer: out,
Total: int64(size),
Callback: callback,
}
// Write the body to file with progress
_, err = io.Copy(pw, resp.Body)
if err != nil {
return err
}
fmt.Println("\nDownload complete")
return nil
}

View File

@@ -3,7 +3,8 @@ package service
import (
"database/sql"
"fmt"
"log"
"os"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
)
@@ -18,21 +19,50 @@ type MacAddress struct {
}
type SQLiteHelper struct {
connectionString string
db *sql.DB
db *sql.DB
}
func NewSQLiteHelper(dbFilePath string) *SQLiteHelper {
connectionString := fmt.Sprintf("file:%s?cache=shared&mode=rwc", dbFilePath)
db, err := sql.Open("sqlite3", connectionString)
if err != nil {
log.Fatal(err)
// create the instance of SQLiteHelper but not initialize the database connection
func NewSQLiteHelper() *SQLiteHelper {
return &SQLiteHelper{
db: nil,
}
}
// check if the directory and file for the database exist
func (helper *SQLiteHelper) CheckBeforeInit() bool {
userHomeDir, _ := os.UserHomeDir()
dbFileDir := filepath.Join(userHomeDir, ".macfastlookup")
dbFileName := "mac_vendors.sqlite3"
dbFilePath := filepath.Join(dbFileDir, dbFileName)
if _, err := os.Stat(dbFileDir); os.IsNotExist(err) {
os.Mkdir(dbFileDir, 0755)
}
return &SQLiteHelper{
connectionString: connectionString,
db: db,
if _, err := os.Stat(dbFilePath); os.IsNotExist(err) {
return false
}
return true
}
// initialize the database connection
func (helper *SQLiteHelper) InitSQLiteHelper() bool {
dbFileDir := ".macfastlookup"
userHomeDir, _ := os.UserHomeDir()
dbFileName := "mac_vendors.sqlite3"
dbFilePath := filepath.Join(userHomeDir, dbFileDir, dbFileName)
connectionString := fmt.Sprintf("file:%s?cache=shared&mode=rwc", dbFilePath)
db, err := sql.Open("sqlite3", connectionString)
if err != nil {
return false
}
helper.db = db
return true
}
func (helper *SQLiteHelper) Close() {
@@ -40,14 +70,7 @@ func (helper *SQLiteHelper) Close() {
}
func (helper *SQLiteHelper) ReloadDatabase() bool {
helper.db.Close()
db, err := sql.Open("sqlite3", helper.connectionString)
if err != nil {
log.Fatal(err)
return false
}
helper.db = db
return true
return helper.InitSQLiteHelper()
}
func (helper *SQLiteHelper) QueryMacAddressByPrefix(prefix string) (*MacAddress, error) {