~rbn/rssfs

2c0a8a515e40f6b212f42fbf0cac2fd04bff9125 — Ruben Schuller 5 months ago 521b2cb master v0.0.4
use buffer size, fix media enclosures, fix names

- use the buffer size of downloaded media instead of content length
  (shouldv'e done so in the first place)

- allow multiple media enclosures

- replace special chars for filenames
1 files changed, 30 insertions(+), 11 deletions(-)

M rssfs.go
M rssfs.go => rssfs.go +30 -11
@@ 69,6 69,7 @@ import (
	"mime"
	"net"
	"net/http"
	"net/url"
	"os"
	"path"
	"strings"


@@ 86,7 87,8 @@ func main() {
	addr := flags.String("addr", "localhost:9999", "listen address")
	uid := flags.String("uid", "nobody", "uid name")
	gid := flags.String("gid", "nogroup", "gid name")
	debug := flags.Bool("debug", false, "enable debug mode")
	flags.BoolVar(&debug, "debug", false, "enable debug mode")
	debug9 := flags.Bool("debug9", false, "enable 9p debug mode")
	flags.Parse(os.Args[1:])
	urls := flags.Args()



@@ 112,7 114,7 @@ func main() {
		}

		s := neinp.NewServer(r)
		s.Debug = *debug
		s.Debug = *debug9
		s.Serve(conn)
	}
}


@@ 210,6 212,19 @@ func hashVersion(s string) uint32 {
	return h.Sum32()
}

func escape(s string) string {
	f := func(r rune) rune {
		switch {
		case r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '.' || r == '_' || r == '-':
			return r
		default:
			return '_'
		}
	}

	return strings.Map(f, s)
}

type RSSFs struct {
	neinp.NopP2000
	root fs.Entry


@@ 276,7 291,7 @@ func newFeedDir(url, uid, gid string) (*feedDir, error) {
		Atime:  time.Now(),
		Mtime:  time.Now(),
		Length: 0,
		Name:   feed.Title,
		Name:   escape(feed.Title),
		Uid:    uid,
		Gid:    gid,
		Muid:   uid,


@@ 315,7 330,7 @@ func newItemDir(item *gofeed.Item, uid, gid string) (*itemDir, error) {
		Atime:  time.Now(),
		Mtime:  time.Now(),
		Length: 0,
		Name:   item.Title,
		Name:   escape(item.Title),
		Uid:    uid,
		Gid:    gid,
		Muid:   uid,


@@ 332,13 347,19 @@ func newItemDir(item *gofeed.Item, uid, gid string) (*itemDir, error) {
	// if we have enclosures add them, if not check for GUID media.
	switch {
	case len(item.Enclosures) > 0:
		for _, v := range item.Enclosures {
			if mediaUrl(v.URL) {
		for i, v := range item.Enclosures {
			u, err := url.Parse(v.URL)
			if err != nil {
				log.Printf("can't parse enclosure URL %v, skipping", v.URL)
				continue
			}
			u.RawQuery = ""
			if mediaUrl(u.String()) {
				if debug {
					log.Printf("adding enclosure %v as mediaFile", v.URL)
				}

				name := path.Base(v.URL)
				name := fmt.Sprintf("enclosure%v", i)

				media, err := newMediaFile(name, q.Version, time.Now(), v.URL, uid, gid)
				if err != nil {


@@ 407,7 428,7 @@ func newMediaFile(name string, version uint32, times time.Time, url, uid, gid st
		Atime:  times,
		Mtime:  times,
		Length: 0,
		Name:   name,
		Name:   escape(name),
		Uid:    uid,
		Gid:    gid,
		Muid:   uid,


@@ 442,8 463,6 @@ func (m *mediaFile) Open() error {
		}
		defer res.Body.Close()

		m.stat.Length = uint64(res.ContentLength)

		buf, err := ioutil.ReadAll(res.Body)
		if err != nil {
			if debug {


@@ 451,7 470,7 @@ func (m *mediaFile) Open() error {
			}
			return err
		}

		m.stat.Length = uint64(len(buf))
		m.ReadSeeker = bytes.NewReader(buf)
	}
	return nil