| 
									
										
										
										
											2019-01-05 22:44:33 +00:00
										 |  |  | package http | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2020-06-06 15:45:51 +00:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2019-01-05 22:44:33 +00:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2019-05-12 08:04:09 +00:00
										 |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2019-01-05 22:44:33 +00:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2019-05-12 08:04:09 +00:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2019-01-05 22:44:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-06 15:45:51 +00:00
										 |  |  | 	libErrors "github.com/filebrowser/filebrowser/v2/errors" | 
					
						
							| 
									
										
										
										
											2019-01-05 22:44:33 +00:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-31 23:12:36 +00:00
										 |  |  | func renderJSON(w http.ResponseWriter, _ *http.Request, data interface{}) (int, error) { | 
					
						
							| 
									
										
										
										
											2019-01-05 22:44:33 +00:00
										 |  |  | 	marsh, err := json.Marshal(data) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return http.StatusInternalServerError, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	w.Header().Set("Content-Type", "application/json; charset=utf-8") | 
					
						
							|  |  |  | 	if _, err := w.Write(marsh); err != nil { | 
					
						
							|  |  |  | 		return http.StatusInternalServerError, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func errToStatus(err error) int { | 
					
						
							|  |  |  | 	switch { | 
					
						
							|  |  |  | 	case err == nil: | 
					
						
							|  |  |  | 		return http.StatusOK | 
					
						
							|  |  |  | 	case os.IsPermission(err): | 
					
						
							|  |  |  | 		return http.StatusForbidden | 
					
						
							| 
									
										
										
										
											2020-06-06 15:45:51 +00:00
										 |  |  | 	case os.IsNotExist(err), err == libErrors.ErrNotExist: | 
					
						
							| 
									
										
										
										
											2019-01-05 22:44:33 +00:00
										 |  |  | 		return http.StatusNotFound | 
					
						
							| 
									
										
										
										
											2020-06-06 15:45:51 +00:00
										 |  |  | 	case os.IsExist(err), err == libErrors.ErrExist: | 
					
						
							| 
									
										
										
										
											2019-01-05 22:44:33 +00:00
										 |  |  | 		return http.StatusConflict | 
					
						
							| 
									
										
										
										
											2020-06-06 15:45:51 +00:00
										 |  |  | 	case errors.Is(err, libErrors.ErrPermissionDenied): | 
					
						
							|  |  |  | 		return http.StatusForbidden | 
					
						
							|  |  |  | 	case errors.Is(err, libErrors.ErrInvalidRequestParams): | 
					
						
							|  |  |  | 		return http.StatusBadRequest | 
					
						
							| 
									
										
										
										
											2019-01-05 22:44:33 +00:00
										 |  |  | 	default: | 
					
						
							|  |  |  | 		return http.StatusInternalServerError | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-12 08:04:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // This is an addaptation if http.StripPrefix in which we don't
 | 
					
						
							|  |  |  | // return 404 if the page doesn't have the needed prefix.
 | 
					
						
							|  |  |  | func stripPrefix(prefix string, h http.Handler) http.Handler { | 
					
						
							| 
									
										
										
										
											2020-05-31 20:24:18 +00:00
										 |  |  | 	if prefix == "" || prefix == "/" { | 
					
						
							| 
									
										
										
										
											2019-05-12 08:04:09 +00:00
										 |  |  | 		return h | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 		p := strings.TrimPrefix(r.URL.Path, prefix) | 
					
						
							|  |  |  | 		r2 := new(http.Request) | 
					
						
							|  |  |  | 		*r2 = *r | 
					
						
							|  |  |  | 		r2.URL = new(url.URL) | 
					
						
							|  |  |  | 		*r2.URL = *r.URL | 
					
						
							|  |  |  | 		r2.URL.Path = p | 
					
						
							|  |  |  | 		h.ServeHTTP(w, r2) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } |