diff --git a/backends/localfs/localfs.go b/backends/localfs/localfs.go index aaf487f..3f48299 100644 --- a/backends/localfs/localfs.go +++ b/backends/localfs/localfs.go @@ -25,6 +25,7 @@ type MetadataJSON struct { Mimetype string `json:"mimetype"` Size int64 `json:"size"` Expiry int64 `json:"expiry"` + SrcIp string `json:"srcip,omitempty"` ArchiveFiles []string `json:"archive_files,omitempty"` } @@ -106,6 +107,8 @@ func (b LocalfsBackend) writeMetadata(key string, metadata backends.Metadata) er Sha256sum: metadata.Sha256sum, Expiry: metadata.Expiry.Unix(), Size: metadata.Size, + SrcIp: metadata.SrcIp, + } dst, err := os.Create(metaPath) @@ -124,7 +127,7 @@ func (b LocalfsBackend) writeMetadata(key string, metadata backends.Metadata) er return nil } -func (b LocalfsBackend) Put(key string, r io.Reader, expiry time.Time, deleteKey, accessKey string) (m backends.Metadata, err error) { +func (b LocalfsBackend) Put(key string, r io.Reader, expiry time.Time, deleteKey, accessKey string, srcIp string) (m backends.Metadata, err error) { filePath := path.Join(b.filesPath, key) dst, err := os.Create(filePath) @@ -153,6 +156,7 @@ func (b LocalfsBackend) Put(key string, r io.Reader, expiry time.Time, deleteKey m.Expiry = expiry m.DeleteKey = deleteKey m.AccessKey = accessKey + m.SrcIp = srcIp m.ArchiveFiles, _ = helpers.ListArchiveFiles(m.Mimetype, m.Size, dst) err = b.writeMetadata(key, m) diff --git a/backends/meta.go b/backends/meta.go index b22276e..1c5868a 100644 --- a/backends/meta.go +++ b/backends/meta.go @@ -12,6 +12,7 @@ type Metadata struct { Mimetype string Size int64 Expiry time.Time + SrcIp string ArchiveFiles []string } diff --git a/backends/storage.go b/backends/storage.go index 864d0a1..4e2ad6b 100644 --- a/backends/storage.go +++ b/backends/storage.go @@ -12,7 +12,7 @@ type StorageBackend interface { Exists(key string) (bool, error) Head(key string) (Metadata, error) Get(key string) (Metadata, io.ReadCloser, error) - Put(key string, r io.Reader, expiry time.Time, deleteKey, accessKey string) (Metadata, error) + Put(key string, r io.Reader, expiry time.Time, deleteKey, accessKey string, srcIp string) (Metadata, error) PutMetadata(key string, m Metadata) error ServeFile(key string, w http.ResponseWriter, r *http.Request) error Size(key string) (int64, error) diff --git a/upload.go b/upload.go index bda0d7f..50ea49a 100644 --- a/upload.go +++ b/upload.go @@ -42,6 +42,7 @@ type UploadRequest struct { deleteKey string // Empty string if not defined randomBarename bool accessKey string // Empty string if not defined + srcIp string // Empty string if not defined } // Metadata associated with a file as it would actually be stored @@ -333,8 +334,8 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) { if upReq.deleteKey == "" { upReq.deleteKey = uniuri.NewLen(30) } - - upload.Metadata, err = storageBackend.Put(upload.Filename, io.MultiReader(bytes.NewReader(header), upReq.src), fileExpiry, upReq.deleteKey, upReq.accessKey) + var srcIp = r.Header.get("X-Forwarded-For") + upload.Metadata, err = storageBackend.Put(upload.Filename, io.MultiReader(bytes.NewReader(header), upReq.src), fileExpiry, upReq.deleteKey, upReq.accessKey, srcIp) if err != nil { return upload, err }