Пример #1
0
int upload_file(const char * from, const char * id, const char * to, char * new_id, size_t id_sz, CheckSum & md5) {
  fprintf(rlog, "uploading %s%s -> %s\n", id ? "id: " : "", id ? id : from, to);

  auto src = fopen(from, "r");

  if (!src) {
    fprintf(rlog, "ERROR: could not open \"%s\" for reading\n", from);
    return -1;
  }

  char url[1024];
  snprintf(url, 1024, "upload/drive/v3/files%s%s?uploadType=resumable&fields=name,id,size,md5Checksum",
	   id ? "/" : "", id ? id : "");

  DriveApi upload{id ? PATCH : POST, url};
  upload.headers.push_back("X-Upload-Content-Type: application/octet-stream");

  struct stat st = {};
  stat(from, &st);

  if (!id)
    upload.params.AddMember("parents", Value().SetArray().PushBack(StringRef(root_id), upload.params.doc.GetAllocator()));
  if (to)
    upload.params.AddMember("name", StringRef(to));
  char date[32];
  strftime(date, 32, "%Y-%m-%dT%TZ", gmtime(&st.st_mtime));
  upload.params.AddMember("modifiedTime", StringRef(date));

  upload.include_headers = true;

  auto resp = upload.perform();
  if (report_error("upload step 1", resp) != 0) {
    fclose(src);
    return -1;
  }

  //printf("Success\n");
  //printf("%s\n", resp.str.c_str());

  auto location = get_http_header("Location", resp.str.c_str());

  DriveApi put{PUT, location.c_str()};
  put.headers.push_back("Content-Type: application/octet-stream");
  put.body_fh = src;
  resp = put.perform();
  fclose(src);
  HANDLE_ERROR("upload step 2", resp);

  md5 = resp.data["md5Checksum"].GetString();
  
  //printf("Upload Success\n");
  //printf("%s\n", resp.str.c_str());
  
  ///return {resp.data["id"].asString(), atoi(resp.data["size"].asCString()), resp.data["md5Checksum"].asString()};

  if (new_id)
    strncpy(new_id, resp.data["id"].GetString(), id_sz);
 
  return 0;
}
Пример #2
0
int rename_file(const char * id, const char * to, time_t mtime) {
  fprintf(rlog, "renaming id:%s -> %s\n", id, to);
  char url[1024];
  snprintf(url, 1024, "drive/v3/files/%s?fields=name,id,size,md5Checksum", id);
  DriveApi rename{PATCH, url};
  rename.params.AddMember("name", StringRef(to));
  char date[32];
  strftime(date, 32, "%Y-%m-%dT%TZ", gmtime(&mtime));
  rename.params.AddMember("modifiedTime", StringRef(date));
  
  auto resp = rename.perform();
  HANDLE_ERROR("rename", resp);

  //printf("Rename Success!\n");
  //printf("%s\n", resp.str.c_str());

  return 0;
}
Пример #3
0
string drive_mkdir(const char * name) {
  string query;
  add_parm(query, "q", "'root' in parents and name='%s' and trashed = false", name);
  auto resp = DriveApi{GET, "drive/v3/files", query.c_str()}.perform();

  if (resp.data["files"].Size() >= 1) {
    fprintf(rlog, "Folder \"%s\" already exists\n", name);
    exit(1);
  }

  DriveApi mkdir {POST, "drive/v3/files?fields=id"};
  mkdir.params.AddMember("name", StringRef(name));
  mkdir.params.AddMember("mimeType", "application/vnd.google-apps.folder");
  resp = mkdir.perform();
  report_error("mkdir", resp);

  //printf("Success\n");
  //printf("%s\n", resp.str.c_str());

  return resp.data["id"].GetString();
}