files_import.go 4.48 KB
Newer Older
1
//
Christian Pointner's avatar
Christian Pointner committed
2
3
//  tank, Import and Playlist Daemon for Aura project
//  Copyright (C) 2017-2020 Christian Pointner <equinox@helsinki.at>
4
//
Christian Pointner's avatar
Christian Pointner committed
5
6
7
8
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Affero General Public License as
//  published by the Free Software Foundation, either version 3 of the
//  License, or (at your option) any later version.
9
//
Christian Pointner's avatar
Christian Pointner committed
10
//  This program is distributed in the hope that it will be useful,
11
12
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Christian Pointner's avatar
Christian Pointner committed
13
//  GNU Affero General Public License for more details.
14
//
Christian Pointner's avatar
Christian Pointner committed
15
16
//  You should have received a copy of the GNU Affero General Public License
//  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18
19
20
21
22
//

package v1

import (
	"net/http"
23

24
	"github.com/gin-gonic/gin"
25
26
)

robwa's avatar
robwa committed
27
28
29
30
31
32
33
34
// ListImportsOfShow returns a list of all running and pending imports for files belonging to this show.
// @Summary      List imports
// @Description  Lists all running and pending imports
// @Produce      json
// @Param        name    path      string  true   "Name of the show"
// @Param        limit   query     int     false  "Limit number of results"
// @Param        offset  query     int     false  "Start listing from offset"
// @Success      200     {object}  JobsListing
35
// @Failure      400     {object}  ErrorResponse
robwa's avatar
robwa committed
36
37
38
// @Failure      403     {object}  ErrorResponse
// @Failure      500     {object}  ErrorResponse
// @Router       /api/v1/shows/{name}/imports [get]
39
40
func (api *API) ListImportsOfShow(c *gin.Context) {
	showID := c.Param("show-id")
41
	if authorized, _ := authorizeRequestForShow(c, showID); !authorized {
42
43
		return
	}
44
45
46
47
	offset, limit, ok := getPaginationParameter(c)
	if !ok {
		return
	}
48

49
	jobs, err := api.importer.ListJobs(showID, offset, limit)
50
51
52
	if err != nil {
		sendError(c, err)
		return
53
	}
54
	c.JSON(http.StatusOK, JobsListing{jobs})
55
56
}

robwa's avatar
robwa committed
57
58
59
60
61
62
63
64
65
66
67
68
69
// ReadImportOfFile retrieves import status of the file.
// @Summary      Retrieve import status
// @Description  Retrieves import status of the file.
// @Produce      json
// @Param        name      path      string  true   "Name of the show"
// @Param        id        path      int     true   "ID of the file"
// @Param        wait-for  query     string  false  "running|done - If given, return not before import has the given state"
// @Success      200       {object}  importer.Job
// @Failure      400       {object}  ErrorResponse
// @Failure      403       {object}  ErrorResponse
// @Failure      404       {object}  ErrorResponse  "No job for this file"
// @Failure      500       {object}  ErrorResponse
// @Router       /api/v1/shows/{name}/files/{id}/import [get]
70
71
func (api *API) ReadImportOfFile(c *gin.Context) {
	showID := c.Param("show-id")
72
	if authorized, _ := authorizeRequestForShow(c, showID); !authorized {
73
74
		return
	}
75

76
77
78
79
80
	id, err := idFromString(c.Param("file-id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "invalid file-id: " + err.Error()})
		return
	}
81

82
83
84
85
86
87
88
89
90
	waitFor := c.Query("wait-for")
	switch waitFor {
	case "running":
	case "done":
	case "":
	default:
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "unable to wait for unknown state: " + waitFor})
		return
	}
91

92
93
94
95
96
97
	job, err := api.importer.GetJob(showID, id)
	if err != nil {
		// TODO: return import info from store if err == ErrNotFound
		sendError(c, err)
		return
	}
98

99
100
101
102
103
	switch waitFor {
	case "running":
		<-job.Running()
	case "done":
		<-job.Done()
104
	}
105
	c.JSON(http.StatusOK, job)
106
107
}

robwa's avatar
robwa committed
108
109
110
111
112
113
114
115
116
117
118
// CancelImportOfFile cancels import of file.
// @Summary      Cancel file import
// @Description  Cancels import of file.
// @Param        name  path      string  true  "Name of the show"
// @Param        id    path      int     true  "ID of the file"
// @Success      204   {object}  nil
// @Failure      400   {object}  ErrorResponse
// @Failure      403   {object}  ErrorResponse
// @Failure      404   {object}  ErrorResponse  "No job for this file"
// @Failure      500   {object}  ErrorResponse
// @Router       /api/v1/shows/{name}/files/{id}/import [delete]
119
120
func (api *API) CancelImportOfFile(c *gin.Context) {
	showID := c.Param("show-id")
121
	if authorized, _ := authorizeRequestForShow(c, showID); !authorized {
122
123
		return
	}
124

125
126
127
128
129
130
131
132
133
	id, err := idFromString(c.Param("file-id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, ErrorResponse{Error: "invalid file-id: " + err.Error()})
		return
	}
	job, err := api.importer.GetJob(showID, id)
	if err != nil {
		sendError(c, err)
		return
134
	}
135
136
	job.Cancel()
	c.JSON(http.StatusNoContent, nil)
137
}