コード例 #1
0
ファイル: action.go プロジェクト: SchumacherFM/mediamock
func ActionCLI(ctx *cli.Context) {

	var path, outfile string
	path = ctx.String("d")
	outfile = ctx.String("o")

	if false == common.IsDir(path) {
		common.UsageAndExit("Expecting an existing directory: %s", path)
	}

	var cliWriter io.Writer
	cliWriter = os.Stdout
	if ctx.GlobalBool("q") {
		cliWriter = ioutil.Discard
	}

	wc := newWalkCount(path)
	if err := walk.Walk(path, wc.walkFn); err != nil {
		common.UsageAndExit("Walk Counter Error: %s", err)
	}

	if path == "." {
		path = ""
	}

	w := newTraverse(cliWriter, path, outfile, wc.fileCount)
	defer w.close()
	if err := walk.Walk(path, w.walkFn); err != nil {
		common.UsageAndExit("Walk Error: %s", err)
	}
}
コード例 #2
0
ファイル: record.go プロジェクト: SchumacherFM/mediamock
func init() {
	luximrTTF, err := truetype.Parse(luximr)
	if err != nil {
		common.UsageAndExit("failed to parse luximir font: %s", err)
	}
	luximr = nil // kill 72Kb of font data

	draw2d.RegisterFont(
		draw2d.FontData{Name: "luxi", Family: draw2d.FontFamilyMono, Style: draw2d.FontStyleNormal},
		luximrTTF,
	)
}
コード例 #3
0
ファイル: proxy.go プロジェクト: SchumacherFM/mediamock
func (p *proxy) serveAndSaveRemoteFile(w http.ResponseWriter, r *http.Request) bool {
	reqFile := r.URL.Path[1:]
	cachedFile := p.cacheDir + reqFile
	remoteFile := p.url + reqFile

	resp, err := http.Get(remoteFile)
	defer func() {
		if resp.Body != nil {
			if err := resp.Body.Close(); err != nil {
				common.UsageAndExit("Failed to close URL %q with error: %s", remoteFile, err)
			}
		}
	}()

	if err != nil {
		common.InfoErr("Failed to download %q with error: %s\n", remoteFile, err)
		return false
	}
	if resp.StatusCode != http.StatusOK {
		common.InfoErr("Failed to download %q with Code: %q\n", remoteFile, http.StatusText(resp.StatusCode))
		return false
	}

	if dcf := filepath.Dir(cachedFile); false == common.IsDir(dcf) {
		if err := os.MkdirAll(dcf, 0755); err != nil {
			common.InfoErr("Failed to create cache folder %q with Code: %s\n", dcf, err)
			return false
		}
	}

	fw, err := os.OpenFile(cachedFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
	if err != nil {
		common.InfoErr("Failed to open write file %q for URL %q with Error: %s\n", cachedFile, remoteFile, err)
		return false
	}
	defer func() {
		if err := fw.Close(); err != nil {
			common.InfoErr("Failed to close write file %q from URL %q with Error %s\n", cachedFile, remoteFile, err)
		}
	}()

	addHeaders(filepath.Ext(reqFile), cacheUntil, w)
	mw := io.MultiWriter(w, fw)
	if _, err := io.Copy(mw, io.LimitReader(resp.Body, maxHttpDownloadSize)); err != nil {
		common.InfoErr("Failed to write to http response and/or file to disk: %q with error: %s. Max Size: %d KBytes\n", remoteFile, err, maxHttpDownloadSize/1024)
		return false
	}

	return true
}
コード例 #4
0
ファイル: csv.go プロジェクト: SchumacherFM/mediamock
func GetCSVContent(csvFile string) [][]string {
	var rawRC io.ReadCloser
	if common.IsHTTP(csvFile) {
		resp, err := http.Get(csvFile)
		if err != nil {
			common.UsageAndExit("Failed to download %q with error: %s", csvFile, err)
		}
		if resp.StatusCode != http.StatusOK {
			common.UsageAndExit("Server return non-200 status code: %s\nFailed to download %s", resp.Status, csvFile)
		}
		rawRC = resp.Body
	} else {
		var err error
		rawRC, err = os.Open(csvFile)
		if err != nil {
			common.UsageAndExit("Failed to open %q with error:%s", csvFile, err)
		}
	}
	defer func() {
		if err := rawRC.Close(); err != nil {
			common.UsageAndExit("Failed to close URL/file %s with error: %s", csvFile, err)
		}
	}()

	rz, err := gzip.NewReader(rawRC)
	if err != nil {
		common.UsageAndExit("Failed to create a GZIP reader from file %s with error: %s", csvFile, err)
	}
	defer func() {
		if err := rz.Close(); err != nil {
			common.UsageAndExit("Failed to close file %q with error: %s", csvFile, err)
		}
	}()

	rc := csv.NewReader(rz)
	rc.Comma = ([]rune(CSVSep))[0]

	records, err := rc.ReadAll()
	if err != nil {
		common.UsageAndExit("Failed to read CSV file %q with error: %s", csvFile, err)
	}

	return records
}
コード例 #5
0
ファイル: traverse.go プロジェクト: SchumacherFM/mediamock
func newTraverse(cliWriter io.Writer, path, outfile string, barMaxCount int) *traverse {
	w := &traverse{
		cliWriter:  cliWriter,
		outfile:    outfile,
		basePath:   path,
		workerRec:  make(chan record.Record),
		workerStop: make(chan struct{}),
		bar:        common.InitPB(barMaxCount),
	}

	var err error
	w.outF, err = os.Create(outfile)
	if err != nil {
		common.UsageAndExit("Failed to open %s with error: %s", outfile, err)
	}
	w.outW = gzip.NewWriter(w.outF)
	// using w.bar.Output with a io.Writer causes some funny print outs.
	w.bar.NotPrint = w.cliWriter == ioutil.Discard
	w.bar.Start()
	go w.workerWriter()
	return w
}
コード例 #6
0
ファイル: record.go プロジェクト: SchumacherFM/mediamock
func (r Record) CreateFile(basePath string) error {

	d, f := r.getDirFile(basePath)
	if err := os.MkdirAll(d, DirPerm); err != nil {
		common.UsageAndExit("Failed to create directory: %s with error: %s", d, err)
	}

	file, err := os.Create(d + f)
	if err != nil {
		return fmt.Errorf("Failed to create file %s%s", d, f)
	}
	defer func() {
		if err := file.Close(); err != nil {
			common.InfoErr("Failed to close file %s with error: %s\n", d+f, err)
		}
	}()

	if err := os.Chtimes(d+f, r.ModTime, r.ModTime); err != nil {
		common.InfoErr("Failed to change time for file %s with error: %s\n", d+f, err)
	}
	r.CreateContent(f, file)

	return nil
}