Exemplo n.º 1
0
func upload_folder(localDirPath string, dirId int64) {
	myFiles, myFolders, err := fs.ListFiles(localDirPath, accept_filter)
	panic_if_error(err)
	for _, f := range myFiles {
		localFilePath := filepath.Join(localDirPath, f.Name())
		//upload to cloud
		fileInfo, err := c.CreateFile(dirId, localFilePath)
		panic_if_error(err)
		upload_file(localFilePath, dirId, fileInfo)
	}

	for _, f := range myFolders {
		localFilePath := filepath.Join(localDirPath, f.Name())
		//upload to cloud
		newFolder, err := c.MakeFolder(dirId, f.Name())
		panic_if_error(err)
		upload_folder(localFilePath, newFolder.Id)
	}
}
Exemplo n.º 2
0
func SyncFolder(dirId int64, dirPath string, dirModTime int64) {
	ok, err := fs.Exists(dirPath)
	panic_if_error(err)
	if !ok {
		os.Mkdir(dirPath, 0755)
	}

	fileList, err := c.FolderList(dirId)
	panic_if_error(err)

	cloudFiles := make(map[string]*alicloud.File)
	cloudFolders := make(map[string]*alicloud.Folder)

	for _, remoteFile := range fileList.Files {
		localFilePath := filepath.Join(dirPath, remoteFile.GetFullName())
		modTime2 := remoteFile.ModifyTime / 1000
		fileInfo1, err := os.Stat(localFilePath)
		var modTime1 int64
		if err != nil && !os.IsNotExist(err) {
			panic_if_error(err)
		}
		if fileInfo1 != nil {
			modTime1 = fileInfo1.ModTime().Unix()
		}

		log.Debugf("+ %s local timestamp: %d remote: %d", localFilePath, modTime1, modTime2)

		if modTime2 > modTime1 {
			fileInfo2, err := c.FileInfo(remoteFile.Id, "", 3)
			panic_if_error(err)

			log.Info("Download:", localFilePath)
			fileInfo2.ModifyTime = remoteFile.ModifyTime
			c.DownloadFile(fileInfo2, localFilePath)
		} else if modTime1 > modTime2 {
			fileInfo2, err := c.FileInfo(remoteFile.Id, "", 3)
			panic_if_error(err)
			fileInfo2, err = c.ModifyFile(remoteFile.Id, dirId, localFilePath, fileInfo2)
			panic_if_error(err)
			upload_file(localFilePath, dirId, fileInfo2)

		}
		cloudFiles[localFilePath] = remoteFile
	}

	for _, d := range fileList.Dirs {
		p := filepath.Join(dirPath, d.Name)
		SyncFolder(d.Id, p, d.ModifyTime)
		cloudFolders[p] = d
	}

	//upload new files from local to cloud
	myFiles, myFolders, _ := fs.ListFiles(dirPath, accept_filter)

	for _, f := range myFiles {
		localFilePath := filepath.Join(dirPath, f.Name())
		if cloudFiles[localFilePath] == nil {
			//upload to cloud
			fileInfo, err := c.CreateFile(dirId, localFilePath)
			panic_if_error(err)
			upload_file(localFilePath, dirId, fileInfo)
		}
	}

	for _, f := range myFolders {
		localFilePath := filepath.Join(dirPath, f.Name())
		if cloudFolders[localFilePath] == nil {
			//upload to cloud
			newFolder, err := c.MakeFolder(dirId, f.Name())
			panic_if_error(err)
			upload_folder(localFilePath, newFolder.Id)
		}
	}

	if dirModTime > 0 {
		err := fs.ChangeModTime(dirPath, dirModTime/1000)
		panic_if_error(err)
	}
}