Beispiel #1
0
func runUpload(cmd *Command, args []string) bool {
	secret := security.Secret(*upload.secretKey)
	if len(cmdUpload.Flag.Args()) == 0 {
		if *upload.dir == "" {
			return false
		}
		filepath.Walk(*upload.dir, func(path string, info os.FileInfo, err error) error {
			if err == nil {
				if !info.IsDir() {
					if *upload.include != "" {
						if ok, _ := filepath.Match(*upload.include, filepath.Base(path)); !ok {
							return nil
						}
					}
					parts, e := operation.NewFileParts([]string{path})
					if e != nil {
						return e
					}
					results, e := operation.SubmitFiles(*upload.master, parts,
						*upload.replication, *upload.collection,
						*upload.ttl, *upload.maxMB, secret)
					bytes, _ := json.Marshal(results)
					fmt.Println(string(bytes))
					if e != nil {
						return e
					}
				}
			} else {
				fmt.Println(err)
			}
			return err
		})
	} else {
		parts, e := operation.NewFileParts(args)
		if e != nil {
			fmt.Println(e.Error())
		}
		results, _ := operation.SubmitFiles(*upload.master, parts,
			*upload.replication, *upload.collection,
			*upload.ttl, *upload.maxMB, secret)
		bytes, _ := json.Marshal(results)
		fmt.Println(string(bytes))
	}
	return true
}
Beispiel #2
0
func doEachCopy(fileOrDir string, host string, path string) bool {
	f, err := os.Open(fileOrDir)
	if err != nil {
		fmt.Printf("Failed to open file %s: %v", fileOrDir, err)
		return false
	}
	defer f.Close()

	fi, err := f.Stat()
	if err != nil {
		fmt.Printf("Failed to get stat for file %s: %v", fileOrDir, err)
		return false
	}

	mode := fi.Mode()
	if mode.IsDir() {
		files, _ := ioutil.ReadDir(fileOrDir)
		for _, subFileOrDir := range files {
			if !doEachCopy(fileOrDir+"/"+subFileOrDir.Name(), host, path+fi.Name()+"/") {
				return false
			}
		}
		return true
	}

	// this is a regular file
	if *copy.include != "" {
		if ok, _ := filepath.Match(*copy.include, filepath.Base(fileOrDir)); !ok {
			return true
		}
	}

	parts, err := operation.NewFileParts([]string{fileOrDir})
	if err != nil {
		fmt.Printf("Failed to read file %s: %v", fileOrDir, err)
	}

	results, err := operation.SubmitFiles(*copy.master, parts,
		*copy.replication, *copy.collection,
		*copy.ttl, *copy.maxMB, copy.secret)
	if err != nil {
		fmt.Printf("Failed to submit file %s: %v", fileOrDir, err)
	}

	if strings.HasSuffix(path, "/") {
		path = path + fi.Name()
	}

	if err = filer_operation.RegisterFile(host, path, results[0].Fid, copy.secret); err != nil {
		fmt.Printf("Failed to register file %s on %s: %v", fileOrDir, host, err)
		return false
	}

	fmt.Printf("Copy %s => http://%s%s\n", fileOrDir, host, path)

	return true
}