github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/backup.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package controllers 20 21 import ( 22 "github.com/e154/smart-home/common/apperr" 23 "github.com/labstack/echo/v4" 24 25 "github.com/e154/smart-home/api/stub" 26 ) 27 28 const maxMemory = 128 << 20 29 30 // ControllerBackup ... 31 type ControllerBackup struct { 32 *ControllerCommon 33 } 34 35 // NewControllerBackup ... 36 func NewControllerBackup(common *ControllerCommon) *ControllerBackup { 37 return &ControllerBackup{ 38 ControllerCommon: common, 39 } 40 } 41 42 // NewBackup ... 43 func (c ControllerBackup) BackupServiceNewBackup(ctx echo.Context, _ stub.BackupServiceNewBackupParams) error { 44 45 err := c.endpoint.Backup.New(ctx.Request().Context()) 46 if err != nil { 47 return c.ERROR(ctx, err) 48 } 49 50 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 51 } 52 53 // RestoreBackup ... 54 func (c ControllerBackup) BackupServiceRestoreBackup(ctx echo.Context, name string) error { 55 56 err := c.endpoint.Backup.Restore(ctx.Request().Context(), name) 57 if err != nil { 58 return c.ERROR(ctx, err) 59 } 60 61 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 62 } 63 64 // GetBackupList ... 65 func (c ControllerBackup) BackupServiceGetBackupList(ctx echo.Context, params stub.BackupServiceGetBackupListParams) error { 66 67 pagination := c.Pagination(params.Page, params.Limit, params.Sort) 68 items, total, err := c.endpoint.Backup.GetList(ctx.Request().Context(), pagination) 69 if err != nil { 70 return c.ERROR(ctx, err) 71 } 72 73 return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.Backup.ToBackupListResult(items), total, pagination)) 74 } 75 76 // DeleteBackup ... 77 func (c ControllerBackup) BackupServiceDeleteBackup(ctx echo.Context, name string) error { 78 79 if err := c.endpoint.Backup.Delete(ctx.Request().Context(), name); err != nil { 80 return c.ERROR(ctx, err) 81 } 82 83 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 84 } 85 86 // BackupServiceMuxUploadBackup ... 87 func (c ControllerBackup) BackupServiceUploadBackup(ctx echo.Context, _ stub.BackupServiceUploadBackupParams) error { 88 89 r := ctx.Request() 90 91 if err := r.ParseMultipartForm(maxMemory); err != nil { 92 log.Error(err.Error()) 93 } 94 95 form := r.MultipartForm 96 if len(form.File) == 0 { 97 return c.ERROR(ctx, apperr.ErrInvalidRequest) 98 } 99 100 list, errs, err := c.endpoint.Backup.Upload(r.Context(), form.File) 101 if err != nil { 102 return c.ERROR(ctx, err) 103 } 104 105 var resultBackups = make([]interface{}, 0) 106 107 for _, file := range list { 108 resultBackups = append(resultBackups, map[string]string{ 109 "name": file.Name, 110 }) 111 } 112 113 return c.HTTP200(ctx, map[string]interface{}{ 114 "files": resultBackups, 115 "errors": errs, 116 }) 117 } 118 119 // BackupServiceApplyChanges ... 120 func (c ControllerBackup) BackupServiceApplyState(ctx echo.Context, _ stub.BackupServiceApplyStateParams) error { 121 err := c.endpoint.Backup.ApplyChanges(ctx.Request().Context()) 122 if err != nil { 123 return c.ERROR(ctx, err) 124 } 125 126 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 127 } 128 129 // BackupServiceRevertState ... 130 func (c ControllerBackup) BackupServiceRevertState(ctx echo.Context, _ stub.BackupServiceRevertStateParams) error { 131 err := c.endpoint.Backup.RollbackChanges(ctx.Request().Context()) 132 if err != nil { 133 return c.ERROR(ctx, err) 134 } 135 136 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 137 }