fix delete rec folder
This commit is contained in:
@@ -250,18 +250,16 @@ func (h *ResourceHandler) Get(c *gin.Context) {
|
|||||||
func (h *ResourceHandler) Delete(c *gin.Context) {
|
func (h *ResourceHandler) Delete(c *gin.Context) {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
|
|
||||||
storagePath, _ := h.resources.GetStoragePath(id)
|
result, err := h.resources.DeleteRecursive(id)
|
||||||
variants, _ := h.resources.GetVariantsByResourceID(id)
|
if err != nil {
|
||||||
|
|
||||||
if err := h.resources.Delete(id); err != nil {
|
|
||||||
api.Error(c, http.StatusInternalServerError, "DB_ERROR", "Failed to delete resource")
|
api.Error(c, http.StatusInternalServerError, "DB_ERROR", "Failed to delete resource")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if storagePath != "" {
|
for _, p := range result.StoragePaths {
|
||||||
os.Remove(path.Clean(storagePath))
|
os.Remove(path.Clean(p))
|
||||||
}
|
}
|
||||||
for _, v := range variants {
|
for _, v := range result.Variants {
|
||||||
os.Remove(path.Clean(v.StorageKey))
|
os.Remove(path.Clean(v.StorageKey))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -190,11 +190,57 @@ func (s *ResourceService) Get(id string) (*model.Resource, error) {
|
|||||||
return &m, nil
|
return &m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DeleteResult struct {
|
||||||
|
StoragePaths []string
|
||||||
|
Variants []model.Variant
|
||||||
|
}
|
||||||
|
|
||||||
func (s *ResourceService) Delete(id string) error {
|
func (s *ResourceService) Delete(id string) error {
|
||||||
resourceUUID, _ := uuid.Parse(id)
|
resourceUUID, _ := uuid.Parse(id)
|
||||||
return s.queries.DeleteResource(context.Background(), resourceUUID)
|
return s.queries.DeleteResource(context.Background(), resourceUUID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *ResourceService) DeleteRecursive(id string) (*DeleteResult, error) {
|
||||||
|
resourceUUID, _ := uuid.Parse(id)
|
||||||
|
|
||||||
|
r, err := s.queries.GetResource(context.Background(), resourceUUID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("get resource: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result := &DeleteResult{}
|
||||||
|
|
||||||
|
if r.IsFolder {
|
||||||
|
children, err := s.queries.ListResourcesByParentID(context.Background(), uuid.NullUUID{UUID: resourceUUID, Valid: true})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("list children: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, child := range children {
|
||||||
|
childResult, err := s.DeleteRecursive(child.ID.String())
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("delete child %s: %w", child.ID, err)
|
||||||
|
}
|
||||||
|
result.StoragePaths = append(result.StoragePaths, childResult.StoragePaths...)
|
||||||
|
result.Variants = append(result.Variants, childResult.Variants...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.queries.DeleteResource(context.Background(), resourceUUID); err != nil {
|
||||||
|
return nil, fmt.Errorf("delete resource: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
storagePath, _ := s.GetStoragePath(id)
|
||||||
|
if storagePath != "" {
|
||||||
|
result.StoragePaths = append(result.StoragePaths, storagePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
variants, _ := s.GetVariantsByResourceID(id)
|
||||||
|
result.Variants = append(result.Variants, variants...)
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *ResourceService) GetStoragePath(id string) (string, error) {
|
func (s *ResourceService) GetStoragePath(id string) (string, error) {
|
||||||
resourceUUID, _ := uuid.Parse(id)
|
resourceUUID, _ := uuid.Parse(id)
|
||||||
placement, err := s.queries.GetServerPlacementByResource(context.Background(), resourceUUID)
|
placement, err := s.queries.GetServerPlacementByResource(context.Background(), resourceUUID)
|
||||||
|
|||||||
Reference in New Issue
Block a user