summaryrefslogtreecommitdiff
path: root/vendor/github.com/dsoprea/go-utility/v2/filesystem/calculate_seek.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2022-02-12 18:27:58 +0000
committerLibravatar GitHub <noreply@github.com>2022-02-12 18:27:58 +0000
commit31935ee206107f077878d3cdb6a26b82436b6893 (patch)
tree2d522bf98013dc5a4539133561b645fd7457eb06 /vendor/github.com/dsoprea/go-utility/v2/filesystem/calculate_seek.go
parent[chore] Add nightly mirror to Codeberg.org (#392) (diff)
parentGo mod tidy (diff)
downloadgotosocial-31935ee206107f077878d3cdb6a26b82436b6893.tar.xz
Merge pull request #361 from superseriousbusiness/media_refactorv0.2.0
Refactor media handler to allow async media resolution
Diffstat (limited to 'vendor/github.com/dsoprea/go-utility/v2/filesystem/calculate_seek.go')
-rw-r--r--vendor/github.com/dsoprea/go-utility/v2/filesystem/calculate_seek.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/vendor/github.com/dsoprea/go-utility/v2/filesystem/calculate_seek.go b/vendor/github.com/dsoprea/go-utility/v2/filesystem/calculate_seek.go
new file mode 100644
index 000000000..cd59d727c
--- /dev/null
+++ b/vendor/github.com/dsoprea/go-utility/v2/filesystem/calculate_seek.go
@@ -0,0 +1,52 @@
+package rifs
+
+import (
+ "io"
+ "os"
+
+ "github.com/dsoprea/go-logging"
+)
+
+// SeekType is a convenience type to associate the different seek-types with
+// printable descriptions.
+type SeekType int
+
+// String returns a descriptive string.
+func (n SeekType) String() string {
+ if n == io.SeekCurrent {
+ return "SEEK-CURRENT"
+ } else if n == io.SeekEnd {
+ return "SEEK-END"
+ } else if n == io.SeekStart {
+ return "SEEK-START"
+ }
+
+ log.Panicf("unknown seek-type: (%d)", n)
+ return ""
+}
+
+// CalculateSeek calculates an offset in a file-stream given the parameters.
+func CalculateSeek(currentOffset int64, delta int64, whence int, fileSize int64) (finalOffset int64, err error) {
+ defer func() {
+ if state := recover(); state != nil {
+ err = log.Wrap(state.(error))
+ finalOffset = 0
+ }
+ }()
+
+ if whence == os.SEEK_SET {
+ finalOffset = delta
+ } else if whence == os.SEEK_CUR {
+ finalOffset = currentOffset + delta
+ } else if whence == os.SEEK_END {
+ finalOffset = fileSize + delta
+ } else {
+ log.Panicf("whence not valid: (%d)", whence)
+ }
+
+ if finalOffset < 0 {
+ finalOffset = 0
+ }
+
+ return finalOffset, nil
+}