检查文件是否存在(Dropbox API v2)

问题描述 投票:0回答:1

我正在使用以下技术堆栈开发项目:Angular,Ionic,Cordova。将文件下载到Dropbox时,我需要检查它是否在磁盘上。如果文件已经存在于磁盘上,则需要重命名。我使用[https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata)验证文件的存在。逻辑是这样,如果此方法返回错误,则没有此类文件,因此我将其上传。如果该方法返回元数据,则必须重命名该文件。使用这种方法,对控制台的请求将引发错误(这是自然的)。有没有一种替代方法可以使控制台不会出错?这是实现此方法的一段代码。

  async getNewFileName(fileName: string): Promise<string> {
    const { name, extension } = getNameExtension(fileName);
    for (let i = 0;; i++) {
      const tmpName = i ? `${name}(${i}).${extension}` : fileName;
      if (!await this.checkDropBoxFile(tmpName)) {
        return tmpName;
      }
    }
    return fileName;
  }

  async checkDropBoxFile(fileName: string): Promise<any> {
    const dbx = this.getDropbox();
    try {
      const res = await dbx.filesGetMetadata({ path: '/' + fileName });
      return res;
    } catch (e) {
      return false;
    }
  }
dropbox dropbox-api
1个回答
0
投票

代替在每个文件名上调用/files/get_metadata来检查是否存在,您可以使用/files/list_folder endpoint列出文件夹的所有内容,然后遍历结果以检查文件名。这将消除错误,并且取决于您应用程序的结构,可能会导致对Dropbox API的调用减少。您可以在/files/list_folder中使用Dropbox API explorer端点(和其他端点)玩耍。

© www.soinside.com 2019 - 2024. All rights reserved.