Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
tank
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AURA
tank
Commits
a3524e4c
Commit
a3524e4c
authored
6 years ago
by
Christian Pointner
Browse files
Options
Downloads
Patches
Plain Diff
add api endpoint to fetch file usage
parent
60c646a3
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
api/v1/api.go
+4
-0
4 additions, 0 deletions
api/v1/api.go
api/v1/api_files.go
+17
-0
17 additions, 0 deletions
api/v1/api_files.go
api/v1/types.go
+6
-0
6 additions, 0 deletions
api/v1/types.go
store/files.go
+7
-7
7 additions, 7 deletions
store/files.go
store/types.go
+1
-1
1 addition, 1 deletion
store/types.go
with
35 additions
and
8 deletions
api/v1/api.go
+
4
−
0
View file @
a3524e4c
...
@@ -98,6 +98,10 @@ func InstallHandler(r *mux.Router, st *store.Store, im *importer.Importer, infoL
...
@@ -98,6 +98,10 @@ func InstallHandler(r *mux.Router, st *store.Store, im *importer.Importer, infoL
fileHandler
[
"DELETE"
]
=
api
.
DeleteFileOfGroup
()
fileHandler
[
"DELETE"
]
=
api
.
DeleteFileOfGroup
()
r
.
Handle
(
"/groups/{group-id}/files/{file-id}"
,
fileHandler
)
r
.
Handle
(
"/groups/{group-id}/files/{file-id}"
,
fileHandler
)
usageHandler
:=
make
(
handlers
.
MethodHandler
)
usageHandler
[
"GET"
]
=
api
.
ReadUsageOfFile
()
r
.
Handle
(
"/groups/{group-id}/files/{file-id}/usage"
,
usageHandler
)
importHandler
:=
make
(
handlers
.
MethodHandler
)
importHandler
:=
make
(
handlers
.
MethodHandler
)
importHandler
[
"GET"
]
=
api
.
ReadImportOfFile
()
importHandler
[
"GET"
]
=
api
.
ReadImportOfFile
()
importHandler
[
"PUT"
]
=
api
.
UploadFile
()
importHandler
[
"PUT"
]
=
api
.
UploadFile
()
...
...
This diff is collapsed.
Click to expand it.
api/v1/api_files.go
+
17
−
0
View file @
a3524e4c
...
@@ -121,3 +121,20 @@ func (api *API) DeleteFileOfGroup() http.Handler {
...
@@ -121,3 +121,20 @@ func (api *API) DeleteFileOfGroup() http.Handler {
sendWebResponse
(
w
,
http
.
StatusNoContent
,
nil
)
sendWebResponse
(
w
,
http
.
StatusNoContent
,
nil
)
})
})
}
}
func
(
api
*
API
)
ReadUsageOfFile
()
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
vars
:=
mux
.
Vars
(
r
)
id
,
err
:=
idFromString
(
vars
[
"file-id"
])
if
err
!=
nil
{
sendWebResponse
(
w
,
http
.
StatusBadRequest
,
ErrorResponse
{
Error
:
"invalid file-id: "
+
err
.
Error
()})
return
}
result
:=
FileUsageListing
{}
if
result
.
Usage
.
Playlists
,
err
=
api
.
store
.
GetFileUsage
(
vars
[
"group-id"
],
id
);
err
!=
nil
{
sendStoreError
(
w
,
err
)
return
}
sendWebResponse
(
w
,
http
.
StatusOK
,
result
)
})
}
This diff is collapsed.
Click to expand it.
api/v1/types.go
+
6
−
0
View file @
a3524e4c
...
@@ -55,6 +55,12 @@ type FilesListing struct {
...
@@ -55,6 +55,12 @@ type FilesListing struct {
Files
store
.
Files
`json:"results"`
Files
store
.
Files
`json:"results"`
}
}
type
FileUsageListing
struct
{
Usage
struct
{
Playlists
store
.
Playlists
`json:"playlists"`
}
`json:"results"`
}
type
PlaylistsListing
struct
{
type
PlaylistsListing
struct
{
Playlists
store
.
Playlists
`json:"results"`
Playlists
store
.
Playlists
`json:"results"`
}
}
This diff is collapsed.
Click to expand it.
store/files.go
+
7
−
7
View file @
a3524e4c
...
@@ -74,9 +74,9 @@ func (st *Store) UpdateFile(group string, id uint64, file File) (*File, error) {
...
@@ -74,9 +74,9 @@ func (st *Store) UpdateFile(group string, id uint64, file File) (*File, error) {
return
&
file
,
err
return
&
file
,
err
}
}
func
(
st
*
Store
)
getFileUsage
(
id
uint64
)
(
playlists
Playlists
,
err
error
)
{
func
(
st
*
Store
)
getFileUsage
(
id
uint64
,
playlists
*
Playlists
)
(
err
error
)
{
sub
:=
st
.
db
.
Model
(
PlaylistEntry
{})
.
Select
(
"playlist_id"
)
.
Where
(
"file_id = ?"
,
id
)
.
Group
(
"playlist_id"
)
.
SubQuery
()
sub
:=
st
.
db
.
Model
(
PlaylistEntry
{})
.
Select
(
"playlist_id"
)
.
Where
(
"file_id = ?"
,
id
)
.
Group
(
"playlist_id"
)
.
SubQuery
()
err
=
st
.
db
.
Where
(
"id in ?"
,
sub
)
.
Find
(
&
playlists
)
.
Error
err
=
st
.
db
.
Where
(
"id in ?"
,
sub
)
.
Find
(
playlists
)
.
Error
return
return
}
}
...
@@ -90,7 +90,7 @@ func (st *Store) GetFileUsage(group string, id uint64) (playlists Playlists, err
...
@@ -90,7 +90,7 @@ func (st *Store) GetFileUsage(group string, id uint64) (playlists Playlists, err
if
cnt
==
0
{
if
cnt
==
0
{
return
nil
,
ErrNotFound
return
nil
,
ErrNotFound
}
}
playlists
,
err
=
st
.
getFileUsage
(
id
)
err
=
st
.
getFileUsage
(
id
,
&
playlists
)
return
return
}
}
...
@@ -100,11 +100,11 @@ func (st *Store) DeleteFile(group string, id uint64) (err error) {
...
@@ -100,11 +100,11 @@ func (st *Store) DeleteFile(group string, id uint64) (err error) {
result
:=
st
.
db
.
Where
(
"group_name = ?"
,
group
)
.
Delete
(
&
File
{
ID
:
id
})
result
:=
st
.
db
.
Where
(
"group_name = ?"
,
group
)
.
Delete
(
&
File
{
ID
:
id
})
if
err
=
result
.
Error
;
err
!=
nil
{
if
err
=
result
.
Error
;
err
!=
nil
{
// we assume this is due to a FK constraint -> file in use by playlist_entry
// we assume this is due to a FK constraint -> file in use by playlist_entry
playlists
,
err
:=
st
.
getFileUsage
(
id
)
usageErr
:=
&
ErrFileInUse
{}
if
err
!=
nil
{
if
err
=
st
.
getFileUsage
(
id
,
&
(
usageErr
.
Playlists
));
err
!=
nil
{
return
err
return
}
}
return
&
ErrFileInUse
{
Playlists
:
playlists
}
return
usageErr
}
}
if
result
.
RowsAffected
==
0
{
if
result
.
RowsAffected
==
0
{
return
ErrNotFound
return
ErrNotFound
...
...
This diff is collapsed.
Click to expand it.
store/types.go
+
1
−
1
View file @
a3524e4c
...
@@ -56,7 +56,7 @@ type ErrFileInUse struct {
...
@@ -56,7 +56,7 @@ type ErrFileInUse struct {
}
}
func
(
e
*
ErrFileInUse
)
Error
()
string
{
func
(
e
*
ErrFileInUse
)
Error
()
string
{
return
fmt
.
Sprintf
(
"the file is in use by %d playlist"
,
len
(
e
.
Playlists
))
return
fmt
.
Sprintf
(
"the file is
still
in use by %d playlist
(s)
"
,
len
(
e
.
Playlists
))
}
}
//******* Groups
//******* Groups
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment