//Open a catcher from the given file (creating it if it doesn't exist) and start catching //podcasts func StartCatcher(configSaveLocation string) Catcher { if !pogoutils.FileExists("downloads/") { pogoutils.CreateFolder("downloads") } catcher := Catcher{} _, er := os.Stat(configSaveLocation) if er == nil { contents, err := ioutil.ReadFile(configSaveLocation) if err == nil { json.Unmarshal(contents, &catcher) fmt.Println("Loaded from file") } } else { //Initial creation of a catcher catcher.ConfigLocation = configSaveLocation catcher.RefreshInterval = time.Minute * 30 catcher.SaveData() } //Temporary default for debugging purposes... catcher.RefreshInterval = time.Minute * 2 catcher.addFeed = make(chan PodFeed) catcher.updatedPodcasts = make(chan []PodFeed) catcher.ticker = time.NewTicker(catcher.RefreshInterval) go catcher.Refresher() return catcher }
//Handles the CSS, JS and Bootstrap resources func resHandler(w http.ResponseWriter, r *http.Request) { _, filename := path.Split(r.URL.Path) fullPath := "server/res/" + filename fileExists := pogoutils.FileExists(fullPath) if fileExists { lastModTime := pogoutils.LastMod(fullPath) //This checks whether or not the Header was submitted with //If-Modified-Since, which reduces server IO, only do if Cache is enabled if r.Header["If-Modified-Since"] != nil && Cache { //RFC1123 is the standard date format used with HTTP headerTime, _ := time.Parse(time.RFC1123, r.Header["If-Modified-Since"][0]) if !headerTime.Before(lastModTime) { w.WriteHeader(http.StatusNotModified) return } } //Writer the header and content if Cache { w.Header().Add("Last-Modified", lastModTime.Format(time.RFC1123)) } //Go has a function for serving files easily //I used this function because it reduces the complexity of the code //And it seems to do a good job handling MIME types http.ServeFile(w, r, fullPath) } else { w.WriteHeader(http.StatusNotFound) fmt.Println(fullPath, "not found") } }
//Serves up video/audio func downloadHandler(w http.ResponseWriter, r *http.Request) { _, filename := path.Split(r.URL.Path) if pogoutils.FileExists("downloads/" + filename) { fmt.Println("Serving downloads/", filename) http.ServeFile(w, r, "downloads/"+filename) } }
//Determines whether or not the podcast has been downloaded func (episode PodEpisode) Downloaded() bool { return pogoutils.FileExists(episode.DownloadedFilename()) }