コード例 #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
ファイル: 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
}