2021-05-06 19:35:43 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"github.com/eikenb/pipeat"
|
|
|
|
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/common"
|
2021-07-23 08:19:27 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/vfs"
|
2021-05-06 19:35:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var errTransferAborted = errors.New("transfer aborted")
|
|
|
|
|
|
|
|
type httpdFile struct {
|
|
|
|
*common.BaseTransfer
|
2021-07-23 08:19:27 +00:00
|
|
|
writer io.WriteCloser
|
2021-05-06 19:35:43 +00:00
|
|
|
reader io.ReadCloser
|
|
|
|
isFinished bool
|
|
|
|
}
|
|
|
|
|
2021-07-23 08:19:27 +00:00
|
|
|
func newHTTPDFile(baseTransfer *common.BaseTransfer, pipeWriter *vfs.PipeWriter, pipeReader *pipeat.PipeReaderAt) *httpdFile {
|
|
|
|
var writer io.WriteCloser
|
2021-05-06 19:35:43 +00:00
|
|
|
var reader io.ReadCloser
|
|
|
|
if baseTransfer.File != nil {
|
2021-07-23 08:19:27 +00:00
|
|
|
writer = baseTransfer.File
|
2021-05-06 19:35:43 +00:00
|
|
|
reader = baseTransfer.File
|
2021-07-23 08:19:27 +00:00
|
|
|
} else if pipeWriter != nil {
|
|
|
|
writer = pipeWriter
|
2021-05-06 19:35:43 +00:00
|
|
|
} else if pipeReader != nil {
|
|
|
|
reader = pipeReader
|
|
|
|
}
|
|
|
|
return &httpdFile{
|
|
|
|
BaseTransfer: baseTransfer,
|
2021-07-23 08:19:27 +00:00
|
|
|
writer: writer,
|
2021-05-06 19:35:43 +00:00
|
|
|
reader: reader,
|
|
|
|
isFinished: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads the contents to downloads.
|
|
|
|
func (f *httpdFile) Read(p []byte) (n int, err error) {
|
|
|
|
if atomic.LoadInt32(&f.AbortTransfer) == 1 {
|
|
|
|
return 0, errTransferAborted
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Connection.UpdateLastActivity()
|
|
|
|
|
|
|
|
n, err = f.reader.Read(p)
|
|
|
|
atomic.AddInt64(&f.BytesSent, int64(n))
|
|
|
|
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
f.TransferError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.HandleThrottle()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-23 08:19:27 +00:00
|
|
|
// Write writes the contents to upload
|
|
|
|
func (f *httpdFile) Write(p []byte) (n int, err error) {
|
|
|
|
if atomic.LoadInt32(&f.AbortTransfer) == 1 {
|
|
|
|
return 0, errTransferAborted
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Connection.UpdateLastActivity()
|
|
|
|
|
|
|
|
n, err = f.writer.Write(p)
|
|
|
|
atomic.AddInt64(&f.BytesReceived, int64(n))
|
|
|
|
|
|
|
|
if f.MaxWriteSize > 0 && err == nil && atomic.LoadInt64(&f.BytesReceived) > f.MaxWriteSize {
|
|
|
|
err = common.ErrQuotaExceeded
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
f.TransferError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.HandleThrottle()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
// Close closes the current transfer
|
|
|
|
func (f *httpdFile) Close() error {
|
|
|
|
if err := f.setFinished(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err := f.closeIO()
|
|
|
|
errBaseClose := f.BaseTransfer.Close()
|
|
|
|
if errBaseClose != nil {
|
|
|
|
err = errBaseClose
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Connection.GetFsError(f.Fs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *httpdFile) closeIO() error {
|
|
|
|
var err error
|
|
|
|
if f.File != nil {
|
|
|
|
err = f.File.Close()
|
2021-07-23 08:19:27 +00:00
|
|
|
} else if f.writer != nil {
|
|
|
|
err = f.writer.Close()
|
|
|
|
f.Lock()
|
|
|
|
// we set ErrTransfer here so quota is not updated, in this case the uploads are atomic
|
|
|
|
if err != nil && f.ErrTransfer == nil {
|
|
|
|
f.ErrTransfer = err
|
|
|
|
}
|
|
|
|
f.Unlock()
|
2021-05-06 19:35:43 +00:00
|
|
|
} else if f.reader != nil {
|
|
|
|
err = f.reader.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *httpdFile) setFinished() error {
|
|
|
|
f.Lock()
|
|
|
|
defer f.Unlock()
|
|
|
|
|
|
|
|
if f.isFinished {
|
|
|
|
return common.ErrTransferClosed
|
|
|
|
}
|
|
|
|
f.isFinished = true
|
|
|
|
return nil
|
|
|
|
}
|