]> 127.0.0.1 Git - part/.git/commitdiff
add v0.28.0+20230815479a125
authorqydysky <qydysky@foxmail.com>
Tue, 15 Aug 2023 16:14:26 +0000 (00:14 +0800)
committerqydysky <qydysky@foxmail.com>
Tue, 15 Aug 2023 16:14:26 +0000 (00:14 +0800)
file/FileWR.go
web/Web.go

index a08d630ea7a810b4ba171106ce0c473ef44e26b4..89e18cd4721d73894b5ec57b5b8df2adfa8d4492 100644 (file)
@@ -77,7 +77,7 @@ func (t *File) CopyTo(to *File, byteInSec int64, tryLock bool) error {
        }
        defer to.l.Unlock()
 
-       return transferIO(t.read(), to.write(), byteInSec)
+       return transferIO(t.read(), to.write(), byteInSec, -1)
 }
 
 func (t *File) CopyToIoWriter(to io.Writer, byteInSec int64, tryLock bool) error {
@@ -91,7 +91,21 @@ func (t *File) CopyToIoWriter(to io.Writer, byteInSec int64, tryLock bool) error
        }
        defer t.l.RUnlock()
 
-       return transferIO(t.read(), to, byteInSec)
+       return transferIO(t.read(), to, byteInSec, -1)
+}
+
+func (t *File) CopyToIoWriterUntil(to io.Writer, byteInSec, totalSec int64, tryLock bool) error {
+       t.getRWCloser()
+       if t.Config.AutoClose {
+               defer t.Close()
+       }
+
+       if !t.l.TryRLock() {
+               return ErrFailToLock
+       }
+       defer t.l.RUnlock()
+
+       return transferIO(t.read(), to, byteInSec, totalSec)
 }
 
 func (t *File) Write(data []byte, tryLock bool) (int, error) {
@@ -414,12 +428,12 @@ func newPath(path string, mode fs.FileMode) {
        }
 }
 
-func transferIO(r io.Reader, w io.Writer, byteInSec int64) (e error) {
+func transferIO(r io.Reader, w io.Writer, byteInSec, totalSec int64) (e error) {
        if byteInSec > 0 {
                ticker := time.NewTicker(time.Second)
                defer ticker.Stop()
 
-               for buf := make([]byte, byteInSec); true; {
+               for buf := make([]byte, byteInSec); totalSec < 0 || totalSec > 0; totalSec -= 1 {
                        if n, err := r.Read(buf); n != 0 {
                                if _, werr := w.Write(buf[:n]); werr != nil {
                                        return err
index 4513e64ffe1b2ef0e6b0a33f8f98fd1096c6abf7..79aced2d6714359ae060dae67e0663e82c4b28c1 100644 (file)
@@ -348,6 +348,39 @@ func (t *Cache) Cache(key string, aliveDur time.Duration, w http.ResponseWriter)
        return res
 }
 
+type withflush struct {
+       f  func()
+       h  func() http.Header
+       w  func([]byte) (int, error)
+       wh func(int)
+}
+
+func (t withflush) Header() http.Header {
+       return t.h()
+}
+func (t withflush) Write(b []byte) (i int, e error) {
+       i, e = t.w(b)
+       if t.f != nil {
+               t.f()
+       }
+       return
+}
+func (t withflush) WriteHeader(i int) {
+       t.wh(i)
+}
+
+func WithFlush(w http.ResponseWriter) http.ResponseWriter {
+       if Flusher, ok := w.(http.Flusher); ok {
+               return withflush{
+                       f:  Flusher.Flush,
+                       h:  w.Header,
+                       w:  w.Write,
+                       wh: w.WriteHeader,
+               }
+       }
+       return w
+}
+
 func WithStatusCode(w http.ResponseWriter, code int) {
        w.WriteHeader(code)
        _, _ = w.Write([]byte(http.StatusText(code)))