Skip to content

Commit

Permalink
update: deprecated ioutil functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sreeram-narayanan committed Sep 15, 2023
1 parent 51b4cd7 commit b766513
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
segment, _ := godub.NewLoader().Load(filePath)
fmt.Println(segment)

buf, _ := ioutil.ReadAll()
buf, _ := os.ReadAll()

// Load from buffer, load also accepts `io.Reader` type.
segment, _ = godub.NewLoader().Load(bytes.NewReader(buf))
Expand Down
2 changes: 1 addition & 1 deletion _examples/convert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func doWork(task *Task) {

// 配置下导出参数
err := converter.NewConverter(w).
WithBitRate(fmt.Sprintf("%dk", task.bitRate)).
WithBitRate(task.bitRate).
WithDstFormat(task.dstFormat).
WithChannels(task.numChannel).
WithSampleRate(task.sampleRate).
Expand Down
8 changes: 3 additions & 5 deletions _examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package main

import (
"fmt"
"os"
"path"
"runtime"

"os"

"time"

"github.com/caicloud/nirvana/log"
"github.com/skit-ai/go-dub"
godub "github.com/skit-ai/go-dub"
"github.com/skit-ai/go-dub/converter"
"github.com/skit-ai/vcore/log"
)

func main() {
Expand Down
8 changes: 3 additions & 5 deletions converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package converter
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"runtime"
"strings"

"github.com/skit-ai/go-dub/utils"
"github.com/tink-ab/tempfile"
)

var (
Expand Down Expand Up @@ -171,7 +169,7 @@ func (c *Converter) DstFormat() string {
func (c *Converter) Convert(src interface{}) error {
switch src := src.(type) {
case io.Reader:
file, err := ioutil.TempFile("", "src-file")
file, err := os.CreateTemp("", "src-file")
if err != nil {
return err
}
Expand All @@ -192,7 +190,7 @@ func (c *Converter) Convert(src interface{}) error {
}

func (c *Converter) doConvert() error {
dstFile, err := tempfile.TempFile("", "dst", "."+c.dstFormat)
dstFile, err := TempFile("", "dst", "."+c.dstFormat)
if err != nil {
return err
}
Expand Down Expand Up @@ -224,7 +222,7 @@ func (c *Converter) doConvert() error {
}

// Copy to dst writer
buf, err := ioutil.ReadFile(dstFile.Name())
buf, err := os.ReadFile(dstFile.Name())
c.w.Write(buf)

return err
Expand Down
57 changes: 57 additions & 0 deletions converter/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ package converter

import (
"fmt"
"os"
"path/filepath"
"strconv"
"sync"
"time"

"os/exec"
)

// Random number state.
// We generate random temporary file names so that there's a good
// chance the file doesn't exist yet - keeps the number of tries in
// TempFile to a minimum.
var rand uint32
var randmu sync.Mutex

func GetEncoderName() string {
if IsCommandAvailable(FFMPEGEncoder) {
return FFMPEGEncoder
Expand All @@ -21,3 +33,48 @@ func IsCommandAvailable(name string) bool {
}
return true
}

// TempFile creates a new temporary file in the directory dir with a name
// beginning with prefix and ending with suffix, opens the file for reading and
// writing, and returns the resulting *os.File. If dir is the empty string,
// TempFile uses the default directory for temporary files (see os.TempDir).
// Multiple programs calling TempFile simultaneously will not choose the same
// file. The caller can use f.Name() to find the pathname of the file. It is
// the caller's responsibility to remove the file when no longer needed.
func TempFile(dir, prefix string, suffix string) (f *os.File, err error) {
if dir == "" {
dir = os.TempDir()
}

nconflict := 0
for i := 0; i < 10000; i++ {
name := filepath.Join(dir, prefix+nextSuffix()+suffix)
f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if os.IsExist(err) {
if nconflict++; nconflict > 10 {
randmu.Lock()
rand = reseed()
randmu.Unlock()
}
continue
}
break
}
return
}

func reseed() uint32 {
return uint32(time.Now().UnixNano() + int64(os.Getpid()))
}

func nextSuffix() string {
randmu.Lock()
r := rand
if r == 0 {
r = reseed()
}
r = r*1664525 + 1013904223 // constants from Numerical Recipes
rand = r
randmu.Unlock()
return strconv.Itoa(int(1e9 + r%1e9))[1:]
}
7 changes: 3 additions & 4 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package godub

import (
"fmt"
"os"

"bytes"

"io"

"io/ioutil"

"github.com/skit-ai/go-dub/converter"
"github.com/skit-ai/go-dub/wav"
)
Expand Down Expand Up @@ -36,13 +35,13 @@ func (l *Loader) Load(src interface{}) (*AudioSegment, error) {

switch r := src.(type) {
case io.Reader:
result, err := ioutil.ReadAll(r)
result, err := io.ReadAll(r)
if err != nil {
return nil, err
}
buf = result
case string:
result, err := ioutil.ReadFile(r)
result, err := os.ReadFile(r)
if err != nil {
return nil, err
}
Expand Down
6 changes: 2 additions & 4 deletions wav/decode.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package wav

import (
"bytes"
"encoding/binary"
"fmt"
"io"
"io/ioutil"

"bytes"
)

func Decode(r io.Reader) (*WaveAudio, error) {
Expand All @@ -24,7 +22,7 @@ type Decoder struct {
}

func NewDecoder(r io.Reader) (*Decoder, error) {
buf, err := ioutil.ReadAll(r)
buf, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit b766513

Please sign in to comment.