谷歌驱动器Windows应用程序到/从fileId - 具有相同名称的项目和多个父项

时间:2015-11-16 07:34:55

标签: google-drive-api

我试图将网络上的Google云端硬盘链接(好吧,无论如何都是fileId)转换为硬盘上的Windows Google云端硬盘应用程序路径,然后又回来了。

如果API中存在某些内容(例如,从文件/文件夹ID生成一个不包含C:\Users\[User]\Google Drive\的路径,反之亦然)会有所帮助,但是没有。

到目前为止,我做了:

  1. Windows的ID路径:获取路径的第一个文件夹,并(从根开始)查找匹配的文件夹,然后重复直到完成(可能带有文件名)。问题:项目可以被称为相同的东西,无论是文件或文件夹,还是两者的组合,这在Windows中很棘手。该应用添加了一个数字' (1)'等等,我必须抓住,但我怎么知道哪个项目ID是正确的?我认为编号是基于日期的,但我不确定。所以我最终可能会得到多个结果而无法分辨哪个是哪个。

  2. ID到Windows路径:从ID中获取文件/文件夹的名称,然后继续添加父文件夹,直到我构建路径。问题:与上面的1相同,如果有多个匹配的项目,那么我无法告诉我在翻译到Windows时应该使用哪个。问题:显然,Google云端硬盘中的项目可以包含多个父级。不确定在Windows应用程序中的工作原理。

  3. 任何人都可以帮助我微调我的工作方式,或者告诉我Google云端硬盘应用的具体细节吗?代码是受欢迎的,但不是必需的,我反过来可以提供我需要的代码。

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解这个问题,但无论如何我都试着回答:

1 /假设您有Windows路径,

  

C:\ Users \ User \ Google Drive \ myfile.ext

在GooDrive上创建一个具有类似路径的文件,迭代路径的标记   在GooDrive上递归创建树结构。如果树节点(文件夹/文件)存在,则返回ID,否则创建对象。 GooDrive的主要区别在于标题查询可能会返回多个对象(文件夹/文件列表)。运气不好,你要么使用第一个,要么退出并出错。

  global path = "C:\Users\User\Google Drive\myfile.ext"
  createTree(String path) {
    rootFolderId = create your root or use GooDrive root
    fileId = iterate (firstToken(path, "\"), rootFolderId);
  } 

  iterate(title, parentFolderId) {
    ID (or multiple IDs) = search for title in parentFolderId

    if (multiple IDs exist) 
      BOOM - report error and quit or use the first one

    if (token not last) {
      if (single ID for title exists) {
        folderId = found ID
      } else {  
        folderId = createFolder with title and parentFolderId metadata
      }
      iterate(nextToken(path, "\"), folderId)

    } else {   (last token represent file)
      if (single ID for title exists) {
        fileId = found ID
      } else {  
        fileId = createFile with title and parentFolderId metadata
      }
      return fileId    
    }
  }

你没有指定语言,但是如果它是Java,你可以在 createTree()方法中看到类似的程序here(这是Android代码,所以有一个很多Android特定的goo那里,对不起)

2 /假设您拥有Google云端硬盘 fileId ,您可以使用此伪代码构建Windows路径(从下到上依次为root)。同样,您可能需要处理多个父级(错误或多个路径以及指向单个对象的链接)

  String path = fileId's title

  while () {
    parentID  = get fileId's parent

    if (multiple parentIDs exist) 
      BOOM - report error and quit or construct multiple paths
      (multiple paths would represent file/folder links)

    if (parentID not valid or parentId's title not valid)
      break

    path = parentID's title + "\" + path

     if (parentID's title is your root)
      break
  } 

还有一件事:你说“文件夹和文件可以被称为同一件事......” 在GooDrive中,查看MIME类型,有一个特定的MIME类型“application / vnd.google-apps.folder”,它告诉您它是一个文件夹。此外,任何parentId元数据都代表文件夹,因为文件不能是父文件。

祝你好运