package app import ( "image" "image/jpeg" "io" "os" "strings" ) // UploadImage 将转发所选图像的图片路径。 func (a *WalletApplication) UploadImage() string { filePath := a.RT.Dialog.SelectFile() splitPath := strings.Split(filePath, "/") filename := splitPath[len(splitPath)-1] a.log.Info("Path to user uploaded image: " + filePath) err := CopyFile(filePath, a.paths.ImageDir+filename) if err != nil && filePath != "" { a.log.Errorln("Unable to copy image. ", err) a.sendError("Unable to change Image. ", err) return "None" } file, err := os.Open(filePath) if err != nil && filePath != "" { a.log.Errorln("Unable to open image. ", err) a.sendError("Unable to find Image on the path provided. ", err) return "None" } defer func(file *os.File) { err := file.Close() if err != nil { return } }(file) img, _, err := image.DecodeConfig(file) if err != nil { a.log.Info("Attempting to decode as JPEG") img, err = jpeg.DecodeConfig(file) if err != nil { a.log.Errorln("无法解码图像配置", err) a.sendError("无法更改图像。", err) return "None" } } a.log.Info("Uploaded image resolution is set to ", img.Height, "x", img.Width) if img.Height >= 201 || img.Width >= 201 { a.log.Warnf("图像分辨率太大。不能大于 200x200") return "None" } a.StoreImagePathInDB(filename) return filename } // GetImagePath 从 Login.Vue 中调用。它将查询 DB 以获取用户的个人资料图片并将其返回给 FE 进行展示。 func (a *WalletApplication) GetImagePath() string { if err := a.DB.Model(&a.wallet).Where("wallet_alias = ?", a.wallet.WalletAlias).Error; err != nil { a.log.Errorln("无法查询 Image 路径的数据库记录。原因: ", err) a.sendError("无法查询 Image 路径的数据库记录。原因: ", err) return "" } a.log.Infoln("已选择个人资料图片: ", a.wallet.ProfilePicture) return a.wallet.ProfilePicture } // StoreImagePathInDB 存储个人资料图片在数据库中位置的路径 func (a *WalletApplication) StoreImagePathInDB(path string) bool { if err := a.DB.Model(&a.wallet). Where("wallet_alias = ?", a.wallet.WalletAlias).Update("ProfilePicture", path).Error; err != nil { a.log.Errorln("无法使用 Image path (映像路径) 更新 DB 记录。原因: ", err) a.sendError("无法使用 Image path (映像路径) 更新 DB 记录。原因: ", err) return false } return true } // GetWalletTag 从 Login.Vue 调用 func (a *WalletApplication) GetWalletTag() string { if err := a.DB.Model(&a.wallet).Where("wallet_alias = ?", a.wallet.WalletAlias).Error; err != nil { a.log.Errorln("无法查询 Image 路径的数据库记录。原因: ", err) a.sendError("无法查询 Image 路径的数据库记录。原因: ", err) } a.log.Infoln("已选择 Wallet Tag: ", a.wallet.WalletTag) return a.wallet.WalletTag } // StoreWalletLabelInDB 获取用户输入的钱包标签字符串并将其存储在数据库中 func (a *WalletApplication) StoreWalletLabelInDB(walletTag string) bool { if err := a.DB.Model(&a.wallet).Where("wallet_alias = ?", a.wallet.WalletAlias).Update("WalletTag", walletTag).Error; err != nil { a.log.Errorln("无法使用 wallet 标签更新 DB 记录。原因: ", err) a.sendError("无法使用 wallet 标签更新 DB 记录。原因: ", err) return false } return true } // GetUserTheme 从 Login.Vue 调用 func (a *WalletApplication) GetUserTheme() bool { if err := a.DB.Model(&a.wallet).Where("wallet_alias = ?", a.wallet.WalletAlias).Error; err != nil { a.log.Errorln("无法查询 Image 路径的数据库记录。原因: ", err) a.sendError("无法查询 Image 路径的数据库记录。原因: ", err) } if a.wallet.DarkMode { a.log.Infoln("启用深色模式") } return a.wallet.DarkMode } // StoreDarkModeStateDB 将暗模式状态存储在用户数据库中 func (a *WalletApplication) StoreDarkModeStateDB(darkMode bool) bool { if err := a.DB.Model(&a.wallet).Where("wallet_alias = ?", a.wallet.WalletAlias).Update("DarkMode", darkMode).Error; err != nil { a.log.Errorln("Unable to store darkmode state. Reason: ", err) a.sendError("Unable to store darkmode state persistently. Reason: ", err) return false } return true } // StoreCurrencyStateDB 将货币状态存储在用户 DB 中 func (a *WalletApplication) StoreCurrencyStateDB(currency string) bool { if err := a.DB.Model(&a.wallet).Where("wallet_alias = ?", a.wallet.WalletAlias).Update("Currency", currency).Error; err != nil { a.log.Errorln("无法存储货币状态。原因: ", err) a.sendError("无法持久存储货币状态。原因: ", err) return false } totalCurrencyBalance := 0.0 if a.wallet.Currency == "USD" { totalCurrencyBalance = float64(a.wallet.Balance) * a.wallet.TokenPrice.DAG.USD } else if a.wallet.Currency == "EUR" { totalCurrencyBalance = float64(a.wallet.Balance) * a.wallet.TokenPrice.DAG.EUR } else if a.wallet.Currency == "BTC" { totalCurrencyBalance = float64(a.wallet.Balance) * a.wallet.TokenPrice.DAG.BTC } a.RT.Events.Emit("totalValue", a.wallet.Currency, totalCurrencyBalance) return true } // UpdateMolly 从前端调用并触发应用程序更新 func (a *WalletApplication) UpdateMolly() { update := new(UpdateWallet) update.app = a update.Run() } // CopyFile the src file to dst. Any existing file will be overwritten and will not // copy file attributes. func CopyFile(src, dst string) error { in, err := os.Open(src) if err != nil { return err } defer func(in *os.File) { err := in.Close() if err != nil { return } }(in) out, err := os.Create(dst) if err != nil { return err } defer func(out *os.File) { err := out.Close() if err != nil { return } }(out) _, err = io.Copy(out, in) if err != nil { return err } return out.Close() }