mirror of
https://github.com/asciinema/asciinema.git
synced 2025-12-16 11:48:13 +01:00
Support playback from http:// and ipfs:// URLs
This commit is contained in:
@@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
## master
|
## master
|
||||||
|
|
||||||
* Added ability to replay asciicast from stdin by passing "-" as filename
|
* Playing asciicast from stdin: `cat demo.json | asciinema play -`
|
||||||
|
* Playing asciicast from IPFS: `asciinema play ipfs://QmcdXYJp6e4zNuimuGeWPwNMHQdxuqWmKx7NhZofQ1nw2V`
|
||||||
* `-q/--quiet` option added to `rec` command
|
* `-q/--quiet` option added to `rec` command
|
||||||
|
|
||||||
## 1.1.1 (2015-06-21)
|
## 1.1.1 (2015-06-21)
|
||||||
|
|||||||
@@ -3,8 +3,11 @@ package asciicast
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Env struct {
|
type Env struct {
|
||||||
@@ -56,25 +59,50 @@ func Save(asciicast *Asciicast, path string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Load(path string) (*Asciicast, error) {
|
// asciinema play file.json
|
||||||
var file *os.File
|
// asciinema play https://asciinema.org/a/123.json
|
||||||
|
// asciinema play ipfs://QmbdpNCwqeZgnmAWBCQcs8u6Ts6P2ku97tfKAycE1XY88p
|
||||||
|
// asciinema play -
|
||||||
|
|
||||||
|
func getSource(url string) (io.ReadCloser, error) {
|
||||||
|
if strings.HasPrefix(url, "ipfs://") {
|
||||||
|
hash := url[7:len(url)]
|
||||||
|
url = fmt.Sprintf("https://ipfs.io/ipfs/%v", hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
if url == "-" {
|
||||||
|
return os.Stdin, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
|
||||||
if path == "-" {
|
|
||||||
file = os.Stdin
|
|
||||||
} else {
|
|
||||||
var err error
|
|
||||||
file, err = os.Open(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
resp.Body.Close()
|
||||||
|
return nil, fmt.Errorf("got status %v when requesting %v", resp.StatusCode, url)
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.Body, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
dec := json.NewDecoder(file)
|
return os.Open(url)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Load(url string) (*Asciicast, error) {
|
||||||
|
source, err := getSource(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer source.Close()
|
||||||
|
|
||||||
|
dec := json.NewDecoder(source)
|
||||||
asciicast := &Asciicast{}
|
asciicast := &Asciicast{}
|
||||||
|
|
||||||
err := dec.Decode(asciicast)
|
if err = dec.Decode(asciicast); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user