drive_api_url_generator.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "google_apis/drive/drive_api_url_generator.h"
  5. #include "base/check_op.h"
  6. #include "base/command_line.h"
  7. #include "base/notreached.h"
  8. #include "base/strings/escape.h"
  9. #include "base/strings/string_number_conversions.h"
  10. #include "base/strings/stringprintf.h"
  11. #include "google_apis/google_api_keys.h"
  12. #include "net/base/url_util.h"
  13. namespace google_apis {
  14. namespace {
  15. // Hard coded URLs for communication with a google drive server.
  16. // TODO(yamaguchi): Make a utility function to compose some of these URLs by a
  17. // version and a resource name.
  18. const char kDriveV2AboutUrl[] = "drive/v2/about";
  19. const char kDriveV2ChangelistUrl[] = "drive/v2/changes";
  20. const char kDriveV2StartPageTokenUrl[] = "drive/v2/changes/startPageToken";
  21. const char kDriveV2FilesUrl[] = "drive/v2/files";
  22. const char kDriveV2FileUrlPrefix[] = "drive/v2/files/";
  23. const char kDriveV2ChildrenUrlFormat[] = "drive/v2/files/%s/children";
  24. const char kDriveV2ChildrenUrlForRemovalFormat[] =
  25. "drive/v2/files/%s/children/%s";
  26. const char kDriveV2FileCopyUrlFormat[] = "drive/v2/files/%s/copy";
  27. const char kDriveV2FileDeleteUrlFormat[] = "drive/v2/files/%s";
  28. const char kDriveV2FileTrashUrlFormat[] = "drive/v2/files/%s/trash";
  29. const char kDriveV2UploadNewFileUrl[] = "upload/drive/v2/files";
  30. const char kDriveV2UploadExistingFileUrlPrefix[] = "upload/drive/v2/files/";
  31. const char kDriveV2BatchUploadUrl[] = "upload/drive";
  32. const char kDriveV2PermissionsUrlFormat[] = "drive/v2/files/%s/permissions";
  33. const char kDriveV2DownloadUrlFormat[] = "drive/v2/files/%s?alt=media";
  34. const char kDriveV2ThumbnailUrlFormat[] = "d/%s=w%d-h%d";
  35. const char kDriveV2ThumbnailUrlWithCropFormat[] = "d/%s=w%d-h%d-c";
  36. const char kDriveV2TeamDrivesUrl[] = "drive/v2/teamdrives";
  37. const char kIncludeTeamDriveItems[] = "includeTeamDriveItems";
  38. const char kSupportsTeamDrives[] = "supportsTeamDrives";
  39. const char kCorpora[] = "corpora";
  40. const char kCorporaAllTeamDrives[] = "default,allTeamDrives";
  41. const char kCorporaDefault[] = "default";
  42. const char kCorporaTeamDrive[] = "teamDrive";
  43. const char kTeamDriveId[] = "teamDriveId";
  44. GURL AddResumableUploadParam(const GURL& url) {
  45. return net::AppendOrReplaceQueryParameter(url, "uploadType", "resumable");
  46. }
  47. GURL AddMultipartUploadParam(const GURL& url) {
  48. return net::AppendOrReplaceQueryParameter(url, "uploadType", "multipart");
  49. }
  50. const char* GetCorporaString(FilesListCorpora corpora) {
  51. switch (corpora) {
  52. case FilesListCorpora::DEFAULT:
  53. return kCorporaDefault;
  54. case FilesListCorpora::TEAM_DRIVE:
  55. return kCorporaTeamDrive;
  56. case FilesListCorpora::ALL_TEAM_DRIVES:
  57. return kCorporaAllTeamDrives;
  58. }
  59. NOTREACHED();
  60. return kCorporaDefault;
  61. }
  62. } // namespace
  63. DriveApiUrlGenerator::DriveApiUrlGenerator(const GURL& base_url,
  64. const GURL& base_thumbnail_url)
  65. : base_url_(base_url), base_thumbnail_url_(base_thumbnail_url) {
  66. // Do nothing.
  67. }
  68. DriveApiUrlGenerator::DriveApiUrlGenerator(const DriveApiUrlGenerator& src) =
  69. default;
  70. DriveApiUrlGenerator::~DriveApiUrlGenerator() {
  71. // Do nothing.
  72. }
  73. const char DriveApiUrlGenerator::kBaseThumbnailUrlForProduction[] =
  74. "https://lh3.googleusercontent.com";
  75. GURL DriveApiUrlGenerator::GetAboutGetUrl() const {
  76. return base_url_.Resolve(kDriveV2AboutUrl);
  77. }
  78. GURL DriveApiUrlGenerator::GetFilesGetUrl(const std::string& file_id,
  79. const GURL& embed_origin) const {
  80. GURL url =
  81. base_url_.Resolve(kDriveV2FileUrlPrefix + base::EscapePath(file_id));
  82. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  83. if (!embed_origin.is_empty()) {
  84. // Construct a valid serialized embed origin from an url, according to
  85. // WD-html5-20110525. Such string has to be built manually, since
  86. // GURL::spec() always adds the trailing slash. Moreover, ports are
  87. // currently not supported.
  88. DCHECK(!embed_origin.has_port());
  89. DCHECK(!embed_origin.has_path() || embed_origin.path() == "/");
  90. const std::string serialized_embed_origin =
  91. embed_origin.scheme() + "://" + embed_origin.host();
  92. url = net::AppendOrReplaceQueryParameter(
  93. url, "embedOrigin", serialized_embed_origin);
  94. }
  95. return url;
  96. }
  97. GURL DriveApiUrlGenerator::GetFilesInsertUrl(
  98. const std::string& visibility) const {
  99. GURL url = base_url_.Resolve(kDriveV2FilesUrl);
  100. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  101. if (!visibility.empty())
  102. url = net::AppendOrReplaceQueryParameter(url, "visibility", visibility);
  103. return url;
  104. }
  105. GURL DriveApiUrlGenerator::GetFilesPatchUrl(const std::string& file_id,
  106. bool set_modified_date,
  107. bool update_viewed_date) const {
  108. GURL url =
  109. base_url_.Resolve(kDriveV2FileUrlPrefix + base::EscapePath(file_id));
  110. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  111. // setModifiedDate is "false" by default.
  112. if (set_modified_date)
  113. url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
  114. // updateViewedDate is "true" by default.
  115. if (!update_viewed_date)
  116. url = net::AppendOrReplaceQueryParameter(url, "updateViewedDate", "false");
  117. return url;
  118. }
  119. GURL DriveApiUrlGenerator::GetFilesCopyUrl(
  120. const std::string& file_id,
  121. const std::string& visibility) const {
  122. GURL url = base_url_.Resolve(base::StringPrintf(
  123. kDriveV2FileCopyUrlFormat, base::EscapePath(file_id).c_str()));
  124. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  125. if (!visibility.empty())
  126. url = net::AppendOrReplaceQueryParameter(url, "visibility", visibility);
  127. return url;
  128. }
  129. GURL DriveApiUrlGenerator::GetFilesListUrl(int max_results,
  130. const std::string& page_token,
  131. FilesListCorpora corpora,
  132. const std::string& team_drive_id,
  133. const std::string& q) const {
  134. GURL url = base_url_.Resolve(kDriveV2FilesUrl);
  135. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  136. url = net::AppendOrReplaceQueryParameter(url, kIncludeTeamDriveItems, "true");
  137. url = net::AppendOrReplaceQueryParameter(url, kCorpora,
  138. GetCorporaString(corpora));
  139. if (!team_drive_id.empty())
  140. url = net::AppendOrReplaceQueryParameter(url, kTeamDriveId, team_drive_id);
  141. // maxResults is 100 by default.
  142. if (max_results != 100) {
  143. url = net::AppendOrReplaceQueryParameter(url, "maxResults",
  144. base::NumberToString(max_results));
  145. }
  146. if (!page_token.empty())
  147. url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token);
  148. if (!q.empty())
  149. url = net::AppendOrReplaceQueryParameter(url, "q", q);
  150. return url;
  151. }
  152. GURL DriveApiUrlGenerator::GetFilesDeleteUrl(const std::string& file_id) const {
  153. GURL url = base_url_.Resolve(base::StringPrintf(
  154. kDriveV2FileDeleteUrlFormat, base::EscapePath(file_id).c_str()));
  155. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  156. return url;
  157. }
  158. GURL DriveApiUrlGenerator::GetFilesTrashUrl(const std::string& file_id) const {
  159. GURL url = base_url_.Resolve(base::StringPrintf(
  160. kDriveV2FileTrashUrlFormat, base::EscapePath(file_id).c_str()));
  161. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  162. return url;
  163. }
  164. GURL DriveApiUrlGenerator::GetChangesListUrl(
  165. bool include_deleted,
  166. int max_results,
  167. const std::string& page_token,
  168. int64_t start_change_id,
  169. const std::string& team_drive_id) const {
  170. DCHECK_GE(start_change_id, 0);
  171. GURL url = base_url_.Resolve(kDriveV2ChangelistUrl);
  172. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  173. url = net::AppendOrReplaceQueryParameter(url, kIncludeTeamDriveItems, "true");
  174. if (!team_drive_id.empty()) {
  175. url = net::AppendOrReplaceQueryParameter(url, kTeamDriveId, team_drive_id);
  176. }
  177. // includeDeleted is "true" by default.
  178. if (!include_deleted)
  179. url = net::AppendOrReplaceQueryParameter(url, "includeDeleted", "false");
  180. // maxResults is "100" by default.
  181. if (max_results != 100) {
  182. url = net::AppendOrReplaceQueryParameter(url, "maxResults",
  183. base::NumberToString(max_results));
  184. }
  185. if (!page_token.empty())
  186. url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token);
  187. if (start_change_id > 0)
  188. url = net::AppendOrReplaceQueryParameter(
  189. url, "startChangeId", base::NumberToString(start_change_id));
  190. return url;
  191. }
  192. GURL DriveApiUrlGenerator::GetChildrenInsertUrl(
  193. const std::string& file_id) const {
  194. GURL url = base_url_.Resolve(base::StringPrintf(
  195. kDriveV2ChildrenUrlFormat, base::EscapePath(file_id).c_str()));
  196. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  197. return url;
  198. }
  199. GURL DriveApiUrlGenerator::GetChildrenDeleteUrl(
  200. const std::string& child_id, const std::string& folder_id) const {
  201. return base_url_.Resolve(base::StringPrintf(
  202. kDriveV2ChildrenUrlForRemovalFormat, base::EscapePath(folder_id).c_str(),
  203. base::EscapePath(child_id).c_str()));
  204. }
  205. GURL DriveApiUrlGenerator::GetInitiateUploadNewFileUrl(
  206. bool set_modified_date) const {
  207. GURL url = AddResumableUploadParam(
  208. base_url_.Resolve(kDriveV2UploadNewFileUrl));
  209. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  210. // setModifiedDate is "false" by default.
  211. if (set_modified_date)
  212. url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
  213. return url;
  214. }
  215. GURL DriveApiUrlGenerator::GetInitiateUploadExistingFileUrl(
  216. const std::string& resource_id,
  217. bool set_modified_date) const {
  218. GURL url = base_url_.Resolve(kDriveV2UploadExistingFileUrlPrefix +
  219. base::EscapePath(resource_id));
  220. url = AddResumableUploadParam(url);
  221. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  222. // setModifiedDate is "false" by default.
  223. if (set_modified_date)
  224. url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
  225. return url;
  226. }
  227. GURL DriveApiUrlGenerator::GetMultipartUploadNewFileUrl(
  228. bool set_modified_date) const {
  229. GURL url = AddMultipartUploadParam(
  230. base_url_.Resolve(kDriveV2UploadNewFileUrl));
  231. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  232. // setModifiedDate is "false" by default.
  233. if (set_modified_date)
  234. url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
  235. return url;
  236. }
  237. GURL DriveApiUrlGenerator::GetMultipartUploadExistingFileUrl(
  238. const std::string& resource_id,
  239. bool set_modified_date) const {
  240. GURL url = base_url_.Resolve(kDriveV2UploadExistingFileUrlPrefix +
  241. base::EscapePath(resource_id));
  242. url = AddMultipartUploadParam(url);
  243. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  244. // setModifiedDate is "false" by default.
  245. if (set_modified_date)
  246. url = net::AppendOrReplaceQueryParameter(url, "setModifiedDate", "true");
  247. return url;
  248. }
  249. GURL DriveApiUrlGenerator::GenerateDownloadFileUrl(
  250. const std::string& resource_id) const {
  251. GURL url = base_url_.Resolve(base::StringPrintf(
  252. kDriveV2DownloadUrlFormat, base::EscapePath(resource_id).c_str()));
  253. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  254. return url;
  255. }
  256. GURL DriveApiUrlGenerator::GetPermissionsInsertUrl(
  257. const std::string& resource_id) const {
  258. GURL url = base_url_.Resolve(base::StringPrintf(
  259. kDriveV2PermissionsUrlFormat, base::EscapePath(resource_id).c_str()));
  260. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  261. return url;
  262. }
  263. GURL DriveApiUrlGenerator::GetThumbnailUrl(const std::string& resource_id,
  264. int width,
  265. int height,
  266. bool crop) const {
  267. return base_thumbnail_url_.Resolve(base::StringPrintf(
  268. crop ? kDriveV2ThumbnailUrlWithCropFormat : kDriveV2ThumbnailUrlFormat,
  269. base::EscapePath(resource_id).c_str(), width, height));
  270. }
  271. GURL DriveApiUrlGenerator::GetBatchUploadUrl() const {
  272. GURL url = base_url_.Resolve(kDriveV2BatchUploadUrl);
  273. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  274. return url;
  275. }
  276. GURL DriveApiUrlGenerator::GetTeamDriveListUrl(
  277. int max_results,
  278. const std::string& page_token) const {
  279. GURL url = base_url_.Resolve(kDriveV2TeamDrivesUrl);
  280. // maxResults is 10 by default.
  281. if (max_results != 10) {
  282. url = net::AppendOrReplaceQueryParameter(url, "maxResults",
  283. base::NumberToString(max_results));
  284. }
  285. if (!page_token.empty())
  286. url = net::AppendOrReplaceQueryParameter(url, "pageToken", page_token);
  287. return url;
  288. }
  289. GURL DriveApiUrlGenerator::GetStartPageTokenUrl(
  290. const std::string& team_drive) const {
  291. GURL url = base_url_.Resolve(kDriveV2StartPageTokenUrl);
  292. url = net::AppendOrReplaceQueryParameter(url, kSupportsTeamDrives, "true");
  293. if (!team_drive.empty())
  294. url = net::AppendOrReplaceQueryParameter(url, kTeamDriveId, team_drive);
  295. return url;
  296. }
  297. } // namespace google_apis