/
folders.go
388 lines (342 loc) · 11.1 KB
/
folders.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package repositories
import (
"context"
"fmt"
"strings"
"github.com/WinterYukky/gorm-extra-clause-plugin/exclause"
"github.com/google/uuid"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/aeroideaservices/focus/media/plugin/actions"
"github.com/aeroideaservices/focus/media/plugin/entity"
"github.com/aeroideaservices/focus/services/errors"
)
// folderRepository репозиторий папок
type folderRepository struct {
db *gorm.DB
}
// NewFolderRepository конструктор
func NewFolderRepository(db *gorm.DB) actions.FolderRepository {
return &folderRepository{db: db}
}
// Has проверка существования папки по id
func (r folderRepository) Has(ctx context.Context, id uuid.UUID) bool {
err := r.db.WithContext(ctx).
Select("id").
Where("id = ?", id).
First(&entity.Folder{}).Error
return !errors.Is(err, gorm.ErrRecordNotFound)
}
// HasByFilter проверка существования папки по фильтру
func (r folderRepository) HasByFilter(ctx context.Context, filter actions.Filter) bool {
db := r.db.WithContext(ctx).Select("id")
db = r.filterFolder(db, filter)
err := db.First(&entity.Folder{}).Error
return !errors.Is(err, gorm.ErrRecordNotFound)
}
// Get получение папки по id
func (r folderRepository) Get(ctx context.Context, id uuid.UUID) (*entity.Folder, error) {
folder := &entity.Folder{}
err := r.db.WithContext(ctx).
Where("id = ?", id).
First(folder).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, actions.ErrFolderNotFound
}
if err != nil {
return nil, errors.NoType.Wrap(err, "error getting folder")
}
return folder, nil
}
// GetWithSize получение папки с размером содержимого
func (r folderRepository) GetWithSize(ctx context.Context, id uuid.UUID) (*actions.FolderDetail, error) {
res := &actions.FolderDetail{}
err := r.db.Table(`
(WITH RECURSIVE tree(id, folder_id, name, size) AS (
SELECT id, folder_id, name,
(SELECT coalesce(SUM(media.size), 0)::bigint FROM media WHERE media.folder_id = folders.id) as size
FROM folders
UNION ALL
SELECT fol.id, fol.folder_id, fol.name, t.size
FROM folders fol
inner join tree t On fol.id = t.folder_id
)
SELECT id, name, folder_id, SUM(size) as size
FROM tree
GROUP BY id, name, folder_id) AS f
`).
WithContext(ctx).
Where("f.id", id).
First(&res).
Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, actions.ErrFolderNotFound
}
if err != nil {
return nil, errors.NoType.Wrap(err, "error getting folder with size")
}
return res, nil
}
// Create создание папки
func (r folderRepository) Create(ctx context.Context, folders ...*entity.Folder) error {
err := r.db.WithContext(ctx).Create(folders).Error
if err != nil {
return errors.NoType.Wrap(err, "error creating folder")
}
return nil
}
// Update обновление папки
func (r folderRepository) Update(ctx context.Context, folder *entity.Folder) error {
err := r.db.Model(folder).WithContext(ctx).
Updates(map[string]any{
"name": folder.Name,
"folder_id": folder.FolderId,
}).
Error
if err != nil {
return errors.NoType.Wrap(err, "error updating folder")
}
return nil
}
// Delete удаление папки
func (r folderRepository) Delete(ctx context.Context, id uuid.UUID) error {
err := r.db.WithContext(ctx).Debug().Where("id = ?", id).Delete(&entity.Folder{}).Error
if err != nil {
return errors.NoType.Wrap(err, "error deleting folder")
}
return nil
}
// List получение списка папок
func (r folderRepository) List(ctx context.Context, filter actions.Filter) ([]*entity.Folder, error) {
var res []*entity.Folder
err := r.db.WithContext(ctx).Debug().
Scopes(r.filterScopes(filter)).
Find(&res).Error
if err != nil {
return nil, errors.NoType.Wrap(err, "error listing folder")
}
return res, nil
}
// GetFolderPath получение пути папки
func (r folderRepository) GetFolderPath(ctx context.Context, id uuid.UUID) (folderPath string, err error) {
var folderNames []string
err = r.db.WithContext(ctx).Raw(
`WITH RECURSIVE parent_folders (id, folder_id, name, folder_level) AS (
SELECT id, folder_id, name, 1 folder_level
FROM folders
WHERE id = ?
UNION ALL
SELECT f.id, f.folder_id, f.name, folder_level + 1
FROM folders f INNER JOIN parent_folders pf
ON f.id = pf.folder_id
)
SELECT name FROM parent_folders
ORDER BY folder_level DESC`, id,
).Scan(&folderNames).Error
if err != nil {
return "", errors.NoType.Wrap(err, "error getting folder path")
}
folderPath = strings.Join(folderNames, "/")
return folderPath, nil
}
// GetFolderMediaFilePaths получение путей медиа файлов
func (r folderRepository) GetFolderMediaFilePaths(ctx context.Context, id *uuid.UUID) (mediaFilepath []string, err error) {
err = r.db.WithContext(ctx).Debug().Raw(
`WITH RECURSIVE parent_folders (id, folder_id) AS (
SELECT id, folder_id
FROM folders
WHERE id = ?
UNION ALL
SELECT f.id, f.folder_id
FROM folders f
INNER JOIN parent_folders pf
ON f.folder_id = pf.id
)
SELECT DISTINCT filepath
FROM media
INNER JOIN parent_folders on parent_folders.id = media.folder_id`, id,
).Scan(&mediaFilepath).Error
if err != nil {
return nil, errors.NoType.Wrap(err, "error getting media filepath")
}
return mediaFilepath, nil
}
// GetAllFolderMedias получение всех медиа
func (r folderRepository) GetAllFolderMedias(ctx context.Context, id uuid.UUID) ([]*actions.UpdateMediaDto, error) {
var medias []*actions.UpdateMediaDto
err := r.db.WithContext(ctx).Debug().Raw(
`WITH RECURSIVE parent_folders (id, folder_id, folder_path) AS (
SELECT id, folder_id, name as folder_path
FROM folders
WHERE folder_id IS NULL
UNION ALL
SELECT f.id, f.folder_id, pf.folder_path || '/' || f.name
FROM folders f
INNER JOIN parent_folders pf
ON f.folder_id = pf.id
)
SELECT DISTINCT media.id, media.name, media.filename, media.filepath, media.folder_id, parent_folders.folder_path || '/' || media.filename as new_filepath
FROM media
INNER JOIN parent_folders on parent_folders.id = media.folder_id
WHERE media.folder_id = ?`, id,
).Scan(&medias).Error
if err != nil {
return nil, errors.NoType.Wrap(err, "error getting folder medias")
}
return medias, nil
}
// HasSubFolder проверка существования поддиректории
func (r folderRepository) HasSubFolder(ctx context.Context, id uuid.UUID, subFolderId *uuid.UUID) (bool, error) {
var hasSubFolder bool
err := r.db.WithContext(ctx).Raw(
`WITH RECURSIVE parent_folders (id, folder_id) AS (
SELECT id, folder_id
FROM folders
WHERE id = ?
UNION ALL
SELECT f.id, f.folder_id
FROM folders f
INNER JOIN parent_folders pf
ON f.folder_id = pf.id
)
SELECT COUNT(1) > 0 count
FROM parent_folders
WHERE id = ?`, id, subFolderId,
).Scan(&hasSubFolder).Error
if err != nil {
return false, errors.NoType.Wrap(err, "error checking if has sub folder")
}
return hasSubFolder, nil
}
// GetFoldersTree получение дерева директорий
func (r folderRepository) GetFoldersTree(ctx context.Context) ([]*actions.FolderResponse, error) {
var entities []*actions.FolderResponse
err := r.db.WithContext(ctx).
Model(entity.Folder{}).
Raw(`
WITH RECURSIVE children_folders(id, name, folder_id, depth_level) AS(
SELECT id, name, folder_id, 1 depth_level
FROM folders
WHERE folder_id IS NULL
UNION ALL
SELECT f.id, f.name, f.folder_id, depth_level + 1
FROM folders f
JOIN children_folders
ON children_folders.id = f.folder_id
)
SELECT id, name, folder_id, depth_level
FROM children_folders
`).
Scan(&entities).Error
if err != nil {
return nil, errors.NoType.Wrap(err, "error getting folder tree")
}
return entities, nil
}
// GetFoldersAndMedias получение папок и медиа
func (r folderRepository) GetFoldersAndMedias(ctx context.Context, filter actions.FolderFilter) (*actions.FoldersAndMediasList, error) {
res := &actions.FoldersAndMediasList{
Total: 0,
Items: []actions.FolderAndMedia{},
}
table := `
(WITH RECURSIVE tree(id, folder_id, name, updated_at, size) AS (
SELECT id, folder_id, name, updated_at,
(SELECT coalesce(SUM(media.size), 0)::bigint FROM media WHERE media.folder_id = id) AS size
FROM folders
UNION ALL
SELECT fol.id, fol.folder_id, fol.name, fol.updated_at, t.size
FROM folders fol
INNER JOIN tree t On fol.id = t.folder_id
)
SELECT id, folder_id, name, filepath, size, updated_at, 'file' AS resource_type
FROM media
UNION
SELECT id, folder_id, name, '', SUM(size), updated_at, 'folder' AS resource_type
FROM tree
GROUP BY id, updated_at, folder_id, name) AS fm
`
sort := "fm." + filter.Sort
if filter.Sort == "" {
sort = "fm.updated_at"
}
order := filter.Order
if filter.Order == "" {
if filter.Sort == "" {
order = "desc"
} else {
order = "asc"
}
}
err := r.db.Table(table).
WithContext(ctx).
Where("fm.folder_id", filter.Filter.FolderId).
Order(fmt.Sprintf("%s %s", sort, order)).
Limit(filter.Limit).Offset(filter.Offset).
Scan(&res.Items).
Error
if err != nil {
return nil, errors.NoType.Wrap(err, "error getting folders & medias")
}
err = r.db.Table(table).
WithContext(ctx).
Where("fm.folder_id", filter.Filter.FolderId).
Count(&res.Total).
Error
if err != nil {
return nil, errors.NoType.Wrap(err, "error getting folders & medias count")
}
return res, nil
}
func (r folderRepository) filterFolder(db *gorm.DB, filter actions.Filter) *gorm.DB {
if filter.WithFolderId {
db = db.Where("folder_id", filter.FolderId)
}
if filter.Name != "" {
db = db.Where("name = ?", filter.Name)
}
return db
}
func (r folderRepository) filterScopes(filter actions.Filter) func(*gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return r.filterFolder(db, filter)
}
}
// GetFolderParents получение родительских папок
func (r folderRepository) GetFolderParents(ctx context.Context, filter actions.Filter) ([]actions.FolderResponse, error) {
res := make([]actions.FolderResponse, 0)
table := "tree"
err := r.db.WithContext(ctx).
Table(table).
Scopes(r.withRecursive(table, filter.FolderId)).
Order(clause.OrderByColumn{
Column: clause.Column{Table: table, Name: "depth"},
Desc: true,
}).Scan(&res).Error
if err != nil {
return nil, errors.NoType.Wrap(err, "error getting parent folders")
}
return res, nil
}
func (r folderRepository) withRecursive(tableName string, folderId *uuid.UUID) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Clauses(exclause.With{
Recursive: true,
CTEs: []exclause.CTE{{
Name: tableName,
Subquery: clause.Expr{
SQL: `
SELECT id, folder_id, name, 1 AS depth
FROM folders
WHERE id = ?
UNION ALL
SELECT f.id, f.folder_id, f.name, depth + 1
FROM folders f INNER JOIN ? pf
ON f.id = pf.folder_id
`,
Vars: []any{folderId, clause.Table{Name: tableName}},
},
}},
})
}
}