// achieve and upload func (b *Builder) publish(file string) (addr string, err error) { var path string if b.framework == "" { path, err = b.pack([]string{file}, filepath.Join(b.srcDir, ".gobuild.yml")) } else { path, err = utils.TempFile("files", "tmp-", "-"+filepath.Base(file)) if err != nil { return } err = sh.Command("mv", "-v", file, path).Run() } if err != nil { return } // file ext<zip|tar.gz> suffix := ".zip" if strings.HasSuffix(path, ".tar.gz") { suffix = ".tar.gz" } go func() { defer func() { log.Debug("delete history:", b.tag) delete(history, b.tag) go func() { // leave 5min gap for unfinished downloading. time.Sleep(time.Minute * 5) //time.Sleep(time.Second * 5) os.Remove(path) }() }() // upload var cdnAddr string var err error if *environment == "development" { cdnAddr, err = UploadLocal(path) } else { name := fmt.Sprintf("%s-%s-%s-%s", filepath.Base(b.project), b.os, b.arch, b.ref) + suffix cdnAddr, err = UploadFile(path, uuid.New()+"/"+name) } if err != nil { return } log.Debug("upload ok:", cdnAddr) output := "" if b.wbc != nil { output = string(b.wbc.Bytes()) } err = database.AddFile(b.pid, b.tag, cdnAddr, output) if err != nil { log.Error(err) } }() tmpAddr := "http://" + opts.Hostname + "/" + path history[b.tag] = tmpAddr return tmpAddr, nil }
func pkgZip(root string, files []string) (path string, err error) { log.Info("package to zip:", path) tmpFile, err := utils.TempFile("files", "tmp-", "-"+filepath.Base(root)+".zip") if err != nil { return } z, err := zip.Create(tmpFile) if err != nil { return } for _, f := range files { var save string if f == "" { continue } // binary file use abspath //fmt.Println(root, f) if strings.HasSuffix(f, root) { save = f[len(root):] } else { save = filepath.Base(f) } info, er := os.Stat(f) if er != nil { continue } log.Debug("add", save, f) if info.IsDir() { if err = z.AddDir(save, f); err != nil { return } } else { if err = z.AddFile(save, f); err != nil { return } } } if err = z.Close(); err != nil { log.Error(err) return } return tmpFile, nil }