func (m *FileSystem) copyFileDocument(f *File, newprefix, oldprefix, newname string, c *mgo.Collection) error { // update parent path prefix with new prefix _parent_path := f.Header.Parent _parent_path = strings.Replace(_parent_path, oldprefix, newprefix, 1) // update header info // both the original and copy will point to the same attachment id // in the bst id, err := filesystem.NewNodeID() if err != nil { return err } f.Header.Parent = _parent_path f.Header.Created = filesystem.FormatDatetime(time.Now()) if newname != "" { err = filesystem.ValidateFileName(newname) if err != nil { return err } f.Header.Name = newname } f.Id = id // save to mongodb err = c.Insert(&f) if err != nil { return err } return nil }
func (m *FileSystem) copyDirectoryDocument(d *Directory, newprefix, oldprefix, newname string, c *mgo.Collection) error { // update parent path prefix with new prefix _parent_path := d.Header.Parent _parent_path = strings.Replace(_parent_path, oldprefix, newprefix, 1) // update header info id, err := filesystem.NewNodeID() if err != nil { return err } d.Header.Parent = _parent_path if newname != "" { err = filesystem.ValidateDirName(newname) if err != nil { return err } d.Header.Name = newname } d.Header.Created = filesystem.FormatDatetime(time.Now()) d.Id = id // save to mongodb err = c.Insert(&d) if err != nil { return err } return nil }
func makeRootDir() (*Directory, error) { id, err := filesystem.NewNodeID() if err != nil { return nil, err } dt := filesystem.FormatDatetime(time.Now()) h := NodeHeader{"/", "Directory", true, dt, ""} r := &Directory{h, id} return r, nil }
func (m *FileSystem) NewDir(p, db string) error { // check path p = path.Clean(p) if p == "/" { err := errors.New("root directory already exists") return err } _name := path.Base(p) _parent := path.Dir(p) err := filesystem.ValidateDirName(_name) if err != nil { return err } // check if parent directory exists q := m.findPathQuery(_parent) // get collection c := m.getBFSCollection(db) var _parentdir Directory // find record err = c.Find(q).One(&_parentdir) if err != nil { return err } if _parentdir.Header.Type != "Directory" { err = fmt.Errorf("directory '%s' couldn't be created: destination isn't a directory.", p) return err } // check if name already taken q = m.findPathQuery(p) _count, err := c.Find(q).Count() if err != nil { return err } if _count > 0 { err = fmt.Errorf("directory '%s' already exists", p) return err } // create directory id, err := filesystem.NewNodeID() if err != nil { return err } dt := filesystem.FormatDatetime(time.Now()) h := NodeHeader{_name, "Directory", false, dt, _parent} _dir := Directory{h, id} // insert node into mongodb err = c.Insert(&_dir) if err != nil { return err } return nil }
func (m *FileSystem) NewFile(p, db string, j map[string]interface{}) error { // check path p = path.Clean(p) _name := path.Base(p) _parent := path.Dir(p) err := filesystem.ValidateFileName(_name) if err != nil { return err } // check if parent directory exists q := m.findPathQuery(_parent) // get collection c := m.getBFSCollection(db) var _parentdir Directory // find record err = c.Find(q).One(&_parentdir) if err != nil { err = fmt.Errorf("destination directory not found: %s", err) return err } if _parentdir.Header.Type != "Directory" { err = errors.New("destination isn't a directory") return err } // check if name already taken q = m.findPathQuery(p) _count, err := c.Find(q).Count() if err != nil { return err } if _count > 0 { err = fmt.Errorf("file '%s' already exists", p) return err } // create file id, err := filesystem.NewNodeID() if err != nil { return err } dt := filesystem.FormatDatetime(time.Now()) h := NodeHeader{_name, "File", false, dt, _parent} a := BytesHeader{"", "", 0} _file := File{h, a, id, j} // insert node into mongodb err = c.Insert(&_file) if err != nil { return err } return nil }