mirror of
				https://github.com/imgproxy/imgproxy.git
				synced 2025-10-30 23:08:02 +02:00 
			
		
		
		
	Simple filesystem transport
This commit is contained in:
		| @@ -10,7 +10,6 @@ import ( | ||||
| 	"io/ioutil" | ||||
| 	"net" | ||||
| 	"net/http" | ||||
| 	"strconv" | ||||
| 	"time" | ||||
|  | ||||
| 	_ "image/gif" | ||||
| @@ -69,7 +68,7 @@ func initDownloading() { | ||||
| 	} | ||||
|  | ||||
| 	if conf.LocalFileSystemRoot != "" { | ||||
| 		transport.RegisterProtocol("local", http.NewFileTransport(http.Dir(conf.LocalFileSystemRoot))) | ||||
| 		transport.RegisterProtocol("local", newFsTransport()) | ||||
| 	} | ||||
|  | ||||
| 	if conf.S3Enabled { | ||||
| @@ -126,9 +125,6 @@ func readAndCheckImage(ctx context.Context, res *http.Response) (context.Context | ||||
|  | ||||
| 	if res.ContentLength > 0 { | ||||
| 		contentLength = int(res.ContentLength) | ||||
| 	} else { | ||||
| 		// ContentLength wasn't set properly, trying to parse the header | ||||
| 		contentLength, _ = strconv.Atoi(res.Header.Get("Content-Length")) | ||||
| 	} | ||||
|  | ||||
| 	buf := downloadBufPool.Get(contentLength) | ||||
|   | ||||
							
								
								
									
										44
									
								
								fs_transport.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								fs_transport.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
| package main | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"net/http" | ||||
| ) | ||||
|  | ||||
| type fsTransport struct { | ||||
| 	fs http.Dir | ||||
| } | ||||
|  | ||||
| func newFsTransport() fsTransport { | ||||
| 	return fsTransport{fs: http.Dir(conf.LocalFileSystemRoot)} | ||||
| } | ||||
|  | ||||
| func (t fsTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) { | ||||
| 	f, err := t.fs.Open(req.URL.Path) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	fi, err := f.Stat() | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	if fi.IsDir() { | ||||
| 		return nil, fmt.Errorf("%s is a directory", req.URL.Path) | ||||
| 	} | ||||
|  | ||||
| 	return &http.Response{ | ||||
| 		Status:        "200 OK", | ||||
| 		StatusCode:    200, | ||||
| 		Proto:         "HTTP/1.0", | ||||
| 		ProtoMajor:    1, | ||||
| 		ProtoMinor:    0, | ||||
| 		Header:        make(http.Header), | ||||
| 		ContentLength: fi.Size(), | ||||
| 		Body:          f, | ||||
| 		Close:         true, | ||||
| 		Request:       req, | ||||
| 	}, nil | ||||
| } | ||||
		Reference in New Issue
	
	Block a user