| 
									
										
										
										
											2020-10-14 18:48:48 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2020-10-16 15:22:24 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2020-10-14 18:48:48 +08:00
										 |  |  | 	"path" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							| 
									
										
										
										
											2020-10-14 18:48:48 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							| 
									
										
										
										
											2020-10-14 18:48:48 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // createShortURL handles requests to create short URLs.
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | func (hs *HTTPServer) createShortURL(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	cmd := dtos.CreateShortURLCmd{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &cmd); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-14 18:48:48 +08:00
										 |  |  | 	hs.log.Debug("Received request to create short URL", "path", cmd.Path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cmd.Path = strings.TrimSpace(cmd.Path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if path.IsAbs(cmd.Path) { | 
					
						
							|  |  |  | 		hs.log.Error("Invalid short URL path", "path", cmd.Path) | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(400, "Path should be relative", nil) | 
					
						
							| 
									
										
										
										
											2020-10-14 18:48:48 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-26 15:07:27 +08:00
										 |  |  | 	if strings.Contains(cmd.Path, "../") { | 
					
						
							|  |  |  | 		hs.log.Error("Invalid short URL path", "path", cmd.Path) | 
					
						
							|  |  |  | 		return response.Error(400, "Invalid path", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-10-14 18:48:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	shortURL, err := hs.ShortURLService.CreateShortURL(c.Req.Context(), c.SignedInUser, cmd.Path) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to create short URL", err) | 
					
						
							| 
									
										
										
										
											2020-10-14 18:48:48 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-01 20:19:10 +08:00
										 |  |  | 	url := fmt.Sprintf("%s/goto/%s?orgId=%d", strings.TrimSuffix(setting.AppUrl, "/"), shortURL.Uid, c.OrgId) | 
					
						
							| 
									
										
										
										
											2020-10-14 18:48:48 +08:00
										 |  |  | 	c.Logger.Debug("Created short URL", "url", url) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dto := dtos.ShortURL{ | 
					
						
							|  |  |  | 		UID: shortURL.Uid, | 
					
						
							|  |  |  | 		URL: url, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-15 20:01:58 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, dto) | 
					
						
							| 
									
										
										
										
											2020-10-14 18:48:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (hs *HTTPServer) redirectFromShortURL(c *models.ReqContext) { | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	shortURLUID := web.Params(c.Req)[":uid"] | 
					
						
							| 
									
										
										
										
											2020-10-14 18:48:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if !util.IsValidShortUID(shortURLUID) { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shortURL, err := hs.ShortURLService.GetShortURLByUID(c.Req.Context(), c.SignedInUser, shortURLUID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		if errors.Is(err, models.ErrShortURLNotFound) { | 
					
						
							|  |  |  | 			hs.log.Debug("Not redirecting short URL since not found") | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		hs.log.Error("Short URL redirection error", "err", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-27 23:16:06 +08:00
										 |  |  | 	// Failure to update LastSeenAt should still allow to redirect
 | 
					
						
							|  |  |  | 	if err := hs.ShortURLService.UpdateLastSeenAt(c.Req.Context(), shortURL); err != nil { | 
					
						
							|  |  |  | 		hs.log.Error("Failed to update short URL last seen at", "error", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-14 18:48:48 +08:00
										 |  |  | 	hs.log.Debug("Redirecting short URL", "path", shortURL.Path) | 
					
						
							|  |  |  | 	c.Redirect(setting.ToAbsUrl(shortURL.Path), 302) | 
					
						
							|  |  |  | } |