func import1(session *http.Session) { session.Stash["indexes"] = indexes session.Stash["Title"] = "SmartPAN Import" m := &ImportForm{} f := form.New(session, m) session.Stash["fh"] = f log.Info("Headers: %s", session.Request.Header()) if session.Request.Method != "POST" { render_import(session) return } f.Populate(true) f.Validate() if f.HasErrors { render_import(session) return } log.Info("Importing into: %s", m.ImportInto) if m.ImportInto == "new_index" { if len(m.NewIndex) == 0 { f.HasErrors = true f.Errors["NewIndex"] = make(map[string]error) f.Errors["NewIndex"]["required"] = errors.New("Please give the new repository a name") render_import(session) return } log.Info("=> Creating new index: %s", m.NewIndex) } b := make([]byte, 20) rand.Read(b) en := base64.URLEncoding d := make([]byte, en.EncodedLen(len(b))) en.Encode(d, b) job := &ImportJob{ Form: m, Complete: false, Id: string(d), Watchers: make([]func(string), 0), } if len(m.Cpanfile) > 0 { log.Info("Got cpanfile:") log.Info(m.Cpanfile) } log.Info("=> Created import job: %s", job.Id) imports[job.Id] = job go do_import(session, job) //render_import(session) if _, ok := session.Request.Form()["stream"]; ok { session.Redirect(&url.URL{Path: "/import/" + job.Id + "/stream", RawQuery: "raw=y"}) } else { session.Redirect(&url.URL{Path: "/import/" + job.Id}) } }
func getindex(session *http.Session) { idx := session.Stash["index"] switch idx { case "CPAN": go func() { config.CPANStatus = "Downloading" res, err := nethttp.Get("https://s3-eu-west-1.amazonaws.com/gopan/cpan_index.gz") if err != nil { log.Error("Error downloading index: %s", err.Error()) session.RenderException(500, errors.New("Error downloading CPAN index: "+err.Error())) config.CPANStatus = "Failed" return } defer res.Body.Close() b, err := ioutil.ReadAll(res.Body) if err != nil { log.Error("Error reading index: %s", err.Error()) session.RenderException(500, errors.New("Error reading CPAN index: "+err.Error())) config.CPANStatus = "Failed" return } fi, err := os.Create(config.CacheDir + "/" + config.CPANIndex) if err != nil { log.Error("Error creating output file: %s", err.Error()) session.RenderException(500, errors.New("Error creating output file: "+err.Error())) config.CPANStatus = "Failed" return } defer fi.Close() fi.Write(b) config.CPANStatus = "Downloaded" config.HasCPANIndex = true config.CPANIndexDate = time.Now().String() config.CPANStatus = "Loading" load_index(config.CPANIndex, config.CacheDir+"/"+config.CPANIndex) config.CPANStatus = "Indexing" update_indexes() config.CPANStatus = "Loaded" }() session.Redirect(&url.URL{Path: "/settings"}) return case "BackPAN": go func() { config.BackPANStatus = "Downloading" res, err := nethttp.Get("https://s3-eu-west-1.amazonaws.com/gopan/backpan_index.gz") if err != nil { log.Error("Error downloading index: %s", err.Error()) session.RenderException(500, errors.New("Error downloading BackPAN index: "+err.Error())) config.BackPANStatus = "Failed" return } defer res.Body.Close() b, err := ioutil.ReadAll(res.Body) if err != nil { log.Error("Error reading index: %s", err.Error()) session.RenderException(500, errors.New("Error reading BackPAN index: "+err.Error())) config.BackPANStatus = "Failed" return } fi, err := os.Create(config.CacheDir + "/" + config.BackPANIndex) if err != nil { log.Error("Error creating output file: %s", err.Error()) session.RenderException(500, errors.New("Error creating output file: "+err.Error())) config.BackPANStatus = "Failed" return } defer fi.Close() fi.Write(b) config.BackPANStatus = "Downloaded" config.HasBackPANIndex = true config.BackPANIndexDate = time.Now().String() config.BackPANStatus = "Loading" load_index(config.BackPANIndex, config.CacheDir+"/"+config.BackPANIndex) config.BackPANStatus = "Indexing" update_indexes() config.BackPANStatus = "Loaded" }() session.Redirect(&url.URL{Path: "/settings"}) return } session.RenderNotFound() }