2022-12-31 10:22:38 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-12-14 10:07:59 +01:00
|
|
|
"errors"
|
2023-11-29 13:13:38 +01:00
|
|
|
"fmt"
|
|
|
|
"image/png"
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-12-31 10:22:38 +01:00
|
|
|
"github.com/doug-martin/goqu/v9"
|
|
|
|
"github.com/google/uuid"
|
2023-11-29 13:13:38 +01:00
|
|
|
"gorm.io/datatypes"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
2023-12-14 10:07:59 +01:00
|
|
|
"github.com/twofas/2fas-server/internal/api/icons/adapters"
|
2023-01-30 19:59:42 +01:00
|
|
|
"github.com/twofas/2fas-server/internal/api/icons/domain"
|
|
|
|
"github.com/twofas/2fas-server/internal/common/storage"
|
2022-12-31 10:22:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type CreateIconRequest struct {
|
|
|
|
Id uuid.UUID
|
|
|
|
CallerId string `json:"caller_id" validate:"required,max=128"`
|
|
|
|
ServiceName string `json:"service_name" validate:"required,max=128"`
|
|
|
|
Issuers []string `json:"issuers" validate:"required,max=128"`
|
2023-02-03 17:00:45 +01:00
|
|
|
Description string `json:"description" validate:"omitempty,max=512"`
|
2022-12-31 10:22:38 +01:00
|
|
|
LightIcon string `json:"light_icon" validate:"required,base64"`
|
|
|
|
DarkIcon string `json:"dark_icon" validate:"omitempty,base64"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateIconRequestHandler struct {
|
|
|
|
Storage storage.FileSystemStorage
|
|
|
|
Repository domain.IconsRequestsRepository
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateIconRequestHandler) Handle(cmd *CreateIconRequest) error {
|
|
|
|
_, rawImg, err := processB64PngImage(cmd.LightIcon)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lightIconPath := filepath.Join(iconsStoragePath, "ir_"+uuid.New().String()+".light.png")
|
|
|
|
|
|
|
|
lightIconLocation, err := h.Storage.Save(lightIconPath, rawImg)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
issuers, err := json.Marshal(cmd.Issuers)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
iconRequest := &domain.IconRequest{
|
|
|
|
Id: cmd.Id,
|
|
|
|
CallerId: cmd.CallerId,
|
|
|
|
Issuers: issuers,
|
|
|
|
ServiceName: cmd.ServiceName,
|
|
|
|
Description: cmd.Description,
|
|
|
|
LightIconUrl: lightIconLocation,
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.DarkIcon != "" {
|
|
|
|
_, darkIconRaw, err := processB64PngImage(cmd.DarkIcon)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
darkIconPath := filepath.Join(iconsStoragePath, "ir_"+uuid.New().String()+".dark.png")
|
|
|
|
|
|
|
|
darkIconLocation, err := h.Storage.Save(darkIconPath, darkIconRaw)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
iconRequest.DarkIconUrl = darkIconLocation
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.Repository.Save(iconRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteIconRequest struct {
|
|
|
|
Id string `uri:"icon_request_id" validate:"required,uuid4"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteIconRequestHandler struct {
|
|
|
|
Repository domain.IconsRequestsRepository
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteIconRequestHandler) Handle(cmd *DeleteIconRequest) error {
|
|
|
|
id, _ := uuid.Parse(cmd.Id)
|
|
|
|
|
|
|
|
icon, err := h.Repository.FindById(id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.Repository.Delete(icon)
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteAllIconsRequestsHandler struct {
|
|
|
|
Database *gorm.DB
|
|
|
|
Qb *goqu.Database
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteAllIconsRequestsHandler) Handle() {
|
|
|
|
sql, _, _ := h.Qb.Truncate("icons_requests").ToSQL()
|
|
|
|
|
|
|
|
h.Database.Exec(sql)
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateWebServiceFromIconRequest struct {
|
|
|
|
IconRequestId string `uri:"icon_request_id" validate:"required,uuid4"`
|
|
|
|
WebServiceId string `json:"web_service_id" validate:"required,uuid4"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateWebServiceFromIconRequestHandler struct {
|
|
|
|
IconsStorage storage.FileSystemStorage
|
|
|
|
WebServiceRepository domain.WebServicesRepository
|
|
|
|
IconsCollectionsRepository domain.IconsCollectionRepository
|
|
|
|
IconsRepository domain.IconsRepository
|
|
|
|
IconsRequestsRepository domain.IconsRequestsRepository
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UpdateWebServiceFromIconRequestHandler) Handle(cmd *UpdateWebServiceFromIconRequest) error {
|
|
|
|
webServiceId, err := uuid.Parse(cmd.WebServiceId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
iconRequestId, err := uuid.Parse(cmd.IconRequestId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
iconRequest, err := h.IconsRequestsRepository.FindById(iconRequestId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
webService, err := h.WebServiceRepository.FindById(webServiceId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lightIconStoragePath := filepath.Join(iconsStoragePath, filepath.Base(iconRequest.LightIconUrl))
|
|
|
|
|
|
|
|
lightIconImg, err := h.IconsStorage.Get(lightIconStoragePath)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to get the icon from the storage: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lightIconPng, err := png.Decode(lightIconImg)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to decode the icon as pgn: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lightIconId := uuid.New()
|
|
|
|
lightIconNewPath := filepath.Join(iconsStoragePath, lightIconId.String()+".png")
|
|
|
|
newLightIconLocation, err := h.IconsStorage.Move(lightIconStoragePath, lightIconNewPath)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to move icons storage: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lightIcon := &domain.Icon{
|
|
|
|
Id: lightIconId,
|
|
|
|
Name: iconRequest.ServiceName,
|
|
|
|
Url: newLightIconLocation,
|
|
|
|
Width: lightIconPng.Bounds().Dx(),
|
|
|
|
Height: lightIconPng.Bounds().Dy(),
|
|
|
|
Type: domain.Light,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.IconsRepository.Save(lightIcon)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to save light icon: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
iconsIds := []string{
|
|
|
|
lightIcon.Id.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if iconRequest.DarkIconUrl != "" {
|
|
|
|
darkIconStoragePath := filepath.Join(iconsStoragePath, filepath.Base(iconRequest.DarkIconUrl))
|
|
|
|
|
|
|
|
darkIconImg, err := h.IconsStorage.Get(darkIconStoragePath)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to get dark icon: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
darkIconPng, err := png.Decode(darkIconImg)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to decode dark icon: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
darkIconId := uuid.New()
|
|
|
|
darkIconNewPath := filepath.Join(iconsStoragePath, darkIconId.String()+".png")
|
|
|
|
newDarkIconLocation, err := h.IconsStorage.Move(darkIconStoragePath, darkIconNewPath)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to move dark icon: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
darkIcon := &domain.Icon{
|
|
|
|
Id: darkIconId,
|
|
|
|
Name: iconRequest.ServiceName,
|
|
|
|
Url: newDarkIconLocation,
|
|
|
|
Width: darkIconPng.Bounds().Dx(),
|
|
|
|
Height: darkIconPng.Bounds().Dy(),
|
|
|
|
Type: domain.Dark,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.IconsRepository.Save(darkIcon)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to save dark icon: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
iconsIds = append(iconsIds, darkIconId.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
iconsJson, err := json.Marshal(iconsIds)
|
2023-11-29 13:13:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to marshal icon ids: %w", err)
|
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
|
2023-12-14 10:07:59 +01:00
|
|
|
var webServiceIconsCollectionsIds []string
|
|
|
|
|
|
|
|
err = json.Unmarshal(webService.IconsCollections, &webServiceIconsCollectionsIds)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decode icons collection from web service: %w", err)
|
|
|
|
}
|
|
|
|
if len(webServiceIconsCollectionsIds) == 1 {
|
|
|
|
if err := h.updateIconsCollection(webServiceIconsCollectionsIds[0], iconRequest.ServiceName, iconsJson); err != nil {
|
|
|
|
return fmt.Errorf("failed to update icons collection %q: %w", webServiceIconsCollectionsIds[0], err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
webService.IconsCollections, err = h.replaceIconsCollections(webServiceIconsCollectionsIds, iconRequest.ServiceName, iconsJson)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to replace icons collections: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := h.WebServiceRepository.Update(webService); err != nil {
|
|
|
|
return fmt.Errorf("failed to update web service %q: %w", webService.Id.String(), err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-14 10:07:59 +01:00
|
|
|
err = h.IconsRequestsRepository.Delete(iconRequest)
|
2022-12-31 10:22:38 +01:00
|
|
|
if err != nil {
|
2023-12-14 10:07:59 +01:00
|
|
|
return fmt.Errorf("failed to delete icon request %q: %w", iconRequest.Id.String(), err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-14 10:07:59 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
|
2023-12-14 10:07:59 +01:00
|
|
|
func (h *UpdateWebServiceFromIconRequestHandler) updateIconsCollection(iconsCollectionID string, name string, iconsJson []byte) error {
|
|
|
|
id, err := uuid.Parse(iconsCollectionID)
|
2022-12-31 10:22:38 +01:00
|
|
|
if err != nil {
|
2023-12-14 10:07:59 +01:00
|
|
|
return fmt.Errorf("invalid icons collection id: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-14 10:07:59 +01:00
|
|
|
return h.IconsCollectionsRepository.Update(&domain.IconsCollection{
|
|
|
|
Id: id,
|
|
|
|
Name: name,
|
|
|
|
Icons: iconsJson,
|
|
|
|
})
|
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
|
2023-12-14 10:07:59 +01:00
|
|
|
func (h *UpdateWebServiceFromIconRequestHandler) replaceIconsCollections(oldCollectionIds []string, serviceName string, iconsJson []byte) (datatypes.JSON, error) {
|
|
|
|
for _, outdatedIconsCollectionId := range oldCollectionIds {
|
|
|
|
if err := h.deleteIconsCollection(outdatedIconsCollectionId); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to delete icons collection %q: %w", outdatedIconsCollectionId, err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
2023-12-14 10:07:59 +01:00
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
|
2023-12-14 10:07:59 +01:00
|
|
|
iconsCollectionId := uuid.New()
|
|
|
|
iconsCollection := &domain.IconsCollection{
|
|
|
|
Id: iconsCollectionId,
|
|
|
|
Name: serviceName,
|
|
|
|
Icons: iconsJson,
|
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
|
2023-12-14 10:07:59 +01:00
|
|
|
if err := h.IconsCollectionsRepository.Save(iconsCollection); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to save new icons collection: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-14 10:07:59 +01:00
|
|
|
iconsCollectionsIds := []string{iconsCollectionId.String()}
|
|
|
|
bb, err := json.Marshal(iconsCollectionsIds)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to marshal icons collections as json: %w", err)
|
|
|
|
}
|
|
|
|
return bb, nil
|
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
|
2023-12-14 10:07:59 +01:00
|
|
|
func (h *UpdateWebServiceFromIconRequestHandler) deleteIconsCollection(collectionID string) error {
|
|
|
|
id, err := uuid.Parse(collectionID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse 'collectionID' %q: %w", collectionID, err)
|
2023-11-29 13:13:38 +01:00
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
|
2023-12-14 10:07:59 +01:00
|
|
|
outDatedIconsCollection, err := h.IconsCollectionsRepository.FindById(id)
|
2022-12-31 10:22:38 +01:00
|
|
|
if err != nil {
|
2023-12-14 10:07:59 +01:00
|
|
|
return fmt.Errorf("failed to find out of date icons collection: %w", err)
|
|
|
|
}
|
|
|
|
err = h.IconsCollectionsRepository.Delete(outDatedIconsCollection)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to delete out of date icons collection: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var outdatedCollectionIcons []string
|
|
|
|
err = json.Unmarshal(outDatedIconsCollection.Icons, &outdatedCollectionIcons)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decode icons ids: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, outdatedIconId := range outdatedCollectionIcons {
|
|
|
|
if err := h.deleteIcon(outdatedIconId); err != nil {
|
|
|
|
return fmt.Errorf("failed to delete icon %q: %w", outdatedIconId, err)
|
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
2023-12-14 10:07:59 +01:00
|
|
|
return nil
|
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
|
2023-12-14 10:07:59 +01:00
|
|
|
func (h *UpdateWebServiceFromIconRequestHandler) deleteIcon(iconIDStr string) error {
|
|
|
|
iconID, err := uuid.Parse(iconIDStr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse iconID: %w", err)
|
|
|
|
}
|
|
|
|
iconToDelete, err := h.IconsRepository.FindById(iconID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed fetch icon by id: %w", err)
|
|
|
|
}
|
|
|
|
if err := h.IconsRepository.Delete(iconToDelete); err != nil {
|
|
|
|
return fmt.Errorf("failed to delete icon: %w", err)
|
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransformIconRequestToWebService struct {
|
|
|
|
WebServiceId uuid.UUID
|
|
|
|
IconRequestId string `uri:"icon_request_id" validate:"required,uuid4"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransformIconRequestToWebServiceHandler struct {
|
|
|
|
IconsStorage storage.FileSystemStorage
|
|
|
|
WebServiceRepository domain.WebServicesRepository
|
|
|
|
IconsRepository domain.IconsRepository
|
|
|
|
IconsCollectionsRepository domain.IconsCollectionRepository
|
|
|
|
IconsRequestsRepository domain.IconsRequestsRepository
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *TransformIconRequestToWebServiceHandler) Handle(cmd *TransformIconRequestToWebService) error {
|
|
|
|
iconRequestId, err := uuid.Parse(cmd.IconRequestId)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("invalid 'iconRequestId': %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
iconRequest, err := h.IconsRequestsRepository.FindById(iconRequestId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-29 13:13:38 +01:00
|
|
|
_, err = h.WebServiceRepository.FindByName(iconRequest.ServiceName)
|
|
|
|
if err == nil {
|
2022-12-31 10:22:38 +01:00
|
|
|
return domain.WebServiceAlreadyExistsError{Name: iconRequest.ServiceName}
|
2023-11-29 13:13:38 +01:00
|
|
|
} else {
|
|
|
|
var notFound adapters.WebServiceCouldNotBeFound
|
2023-12-14 10:07:59 +01:00
|
|
|
if !errors.As(err, ¬Found) {
|
|
|
|
fmt.Printf("Error is: %T %+v\n", err, err)
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to find web service by name: %w", err)
|
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
iconsCollectionId := uuid.New()
|
|
|
|
|
|
|
|
lightIconStoragePath := filepath.Join(iconsStoragePath, filepath.Base(iconRequest.LightIconUrl))
|
|
|
|
|
|
|
|
lightIconImg, err := h.IconsStorage.Get(lightIconStoragePath)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to get light icon: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lightIconPng, err := png.Decode(lightIconImg)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to decode light icon: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lightIconId := uuid.New()
|
|
|
|
lightIconNewPath := filepath.Join(iconsStoragePath, lightIconId.String()+".png")
|
|
|
|
newLightIconLocation, err := h.IconsStorage.Move(lightIconStoragePath, lightIconNewPath)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to move light icon: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lightIcon := &domain.Icon{
|
|
|
|
Id: lightIconId,
|
|
|
|
Name: iconRequest.ServiceName,
|
|
|
|
Url: newLightIconLocation,
|
|
|
|
Width: lightIconPng.Bounds().Dx(),
|
|
|
|
Height: lightIconPng.Bounds().Dy(),
|
|
|
|
Type: domain.Light,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.IconsRepository.Save(lightIcon)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to save light icon: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
iconsIds := []string{
|
|
|
|
lightIcon.Id.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if iconRequest.DarkIconUrl != "" {
|
|
|
|
darkIconStoragePath := filepath.Join(iconsStoragePath, filepath.Base(iconRequest.DarkIconUrl))
|
|
|
|
|
|
|
|
darkIconImg, err := h.IconsStorage.Get(darkIconStoragePath)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to get dark icon: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
darkIconPng, err := png.Decode(darkIconImg)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to decode dark icon: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
darkIconId := uuid.New()
|
|
|
|
darkIconNewPath := filepath.Join(iconsStoragePath, darkIconId.String()+".png")
|
|
|
|
newDarkIconLocation, err := h.IconsStorage.Move(darkIconStoragePath, darkIconNewPath)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to move dark icon: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
darkIcon := &domain.Icon{
|
|
|
|
Id: darkIconId,
|
|
|
|
Name: iconRequest.ServiceName,
|
|
|
|
Url: newDarkIconLocation,
|
|
|
|
Width: darkIconPng.Bounds().Dx(),
|
|
|
|
Height: darkIconPng.Bounds().Dy(),
|
|
|
|
Type: domain.Dark,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.IconsRepository.Save(darkIcon)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to save dark icon: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
iconsIds = append(iconsIds, darkIconId.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
iconsJson, err := json.Marshal(iconsIds)
|
2023-11-29 13:13:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to encode icon ids: %w", err)
|
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
|
|
|
|
iconsCollection := &domain.IconsCollection{
|
|
|
|
Id: iconsCollectionId,
|
|
|
|
Name: iconRequest.ServiceName,
|
|
|
|
Icons: iconsJson,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.IconsCollectionsRepository.Save(iconsCollection)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to save icons collection: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
webService := &domain.WebService{
|
|
|
|
Id: cmd.WebServiceId,
|
|
|
|
Name: iconRequest.ServiceName,
|
|
|
|
Issuers: iconRequest.Issuers,
|
|
|
|
Tags: datatypes.JSON(`[]`),
|
|
|
|
IconsCollections: datatypes.JSON(`["` + iconsCollectionId.String() + `"]`),
|
|
|
|
MatchRules: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.WebServiceRepository.Save(webService)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to save web service: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = h.IconsRequestsRepository.Delete(iconRequest)
|
|
|
|
if err != nil {
|
2023-11-29 13:13:38 +01:00
|
|
|
return fmt.Errorf("failed to delete icon request: %w", err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|