Exemplo n.º 1
0
func (d *MemoryDriver) Rename(from_path string, to_path string) bool {
	if f, from_path_exists := d.Files[from_path]; from_path_exists {
		if _, to_path_exists := d.Files[to_path]; !to_path_exists {
			if _, to_path_parent_exists := d.Files[filepath.Dir(to_path)]; to_path_parent_exists {
				if f.File.IsDir() {
					delete(d.Files, from_path)
					d.Files[to_path] = &MemoryFile{graval.NewDirItem(filepath.Base(to_path)), nil}
					torename := make([]string, 0)
					for p, _ := range d.Files {
						if strings.HasPrefix(p, from_path+"/") {
							torename = append(torename, p)
						}
					}
					for _, p := range torename {
						sf := d.Files[p]
						delete(d.Files, p)
						np := to_path + p[len(from_path):]
						d.Files[np] = sf
					}
				} else {
					delete(d.Files, from_path)
					d.Files[to_path] = &MemoryFile{graval.NewFileItem(filepath.Base(to_path), f.File.Size(), f.File.ModTime()), f.Content}
				}
				return true
			} else {
				return false
			}
		} else {
			return false
		}
	} else {
		return false
	}
}
Exemplo n.º 2
0
// DirContents lists “directory” contents on S3
func (d *S3Driver) DirContents(path string) ([]os.FileInfo, bool) {
	moreObjects := true
	var objects []*s3.Object

	var resp *s3.ListObjectsOutput
	var err error
	marker := ""

	for moreObjects {
		resp, err = d.s3DirContents(path, 1000, marker)

		if err == nil {
			for _, obj := range resp.Contents {
				objects = append(objects, obj)
			}

			moreObjects = *resp.IsTruncated

			if moreObjects {
				last := objects[len(objects)-1]
				marker = *last.Key
			}
		}
	}

	prefix := pathToS3PathPrefix(path)
	var files []os.FileInfo
	var dirs []string

	for _, object := range objects {
		p := *object.Key

		p = strings.TrimPrefix(p, *prefix)
		var fi os.FileInfo

		if strings.Contains(p, "/") || p == "" {

			parts := strings.Split(p, "/")
			dirPart := parts[0]

			if dirPart != d.WorkingDirectory && dirPart != "" && dirPart != "/" && !stringInSlice(dirPart, dirs) {
				fi = graval.NewDirItem(dirPart)
				files = append(files, fi)

				dirs = append(dirs, dirPart)
			}
		} else {
			fi = graval.NewFileItem(*object.Key, *object.Size, *object.LastModified)
			files = append(files, fi)
		}
	}

	return files, true
}
Exemplo n.º 3
0
func (d *MemoryDriver) PutFile(path string, reader io.Reader) bool {
	if _, path_exists := d.Files[path]; !path_exists {
		if _, path_parent_exists := d.Files[filepath.Dir(path)]; path_parent_exists {
			bytes, err := ioutil.ReadAll(reader)
			if err != nil {
				return false
			}

			d.Files[path] = &MemoryFile{graval.NewFileItem(filepath.Base(path), int64(len(bytes)), time.Now().UTC()), bytes}

			return true
		} else {
			return false
		}
	} else {
		return false
	}
}