コード例 #1
0
 /// <summary>
 /// Creates a file on Google Drive : GoogleDriveService.Files.Create
 /// </summary>
 /// <param name="service"></param>
 /// <param name="filePath"></param>
 /// <returns></returns>
 ///
 public GoogleDriveManagerResult UploadFile(DriveService service, LocalGoogleDriveFile localGoogleDriveFile, string folderId = null)
 {
     result = new GoogleDriveManagerResult();
     try
     {
         Google.Apis.Drive.v3.Data.File  uploadedFile = null;
         Google.Apis.Drive.v3.Data.File  fileMetaData = null;
         FilesResource.CreateMediaUpload request;
         List <string> parents = null;
         if (folderId != null)
         {
             parents = new List <string> {
                 folderId
             };
             fileMetaData = GetFileMetaData(localGoogleDriveFile, parents);
         }
         else
         {
             fileMetaData = GetFileMetaData(localGoogleDriveFile);
         }
         using (var fileStream = new System.IO.FileStream(localGoogleDriveFile.FilePath, System.IO.FileMode.Open))
         {
             request        = service.Files.Create(fileMetaData, fileStream, fileMetaData.MimeType);
             request.Fields = "id";
             request.Upload();
         }
         uploadedFile = request.ResponseBody;
         WriteToConsole(GoogleDriveManagementConstants.FileIdReturned + uploadedFile.Id);
         result.GoogleDriveFileId = uploadedFile.Id;
         return(result);
     }
     catch (Exception e)
     {
         result.GoogleDriveFileId = string.Empty;
         Console.WriteLine(GoogleDriveManagementConstants.GettingFileListException + e.Message);
         result.Exceptions.Add(e);
         return(result);
     }
 }
コード例 #2
0
        private Google.Apis.Drive.v3.Data.File GetFileMetaData(LocalGoogleDriveFile localGoogleDriveFile, IList <string> parents = null)
        {
            var fileMetaData = new Google.Apis.Drive.v3.Data.File();

            fileMetaData.Name = Path.GetFileName(Path.GetFileName(localGoogleDriveFile.FilePath));
            if (parents != null)
            {
                fileMetaData.Parents = parents;
            }
            var mimeType = System.Web.MimeMapping.GetMimeMapping(localGoogleDriveFile.FilePath);

            if (string.IsNullOrEmpty(mimeType))
            {
                fileMetaData.MimeType = GoogleDriveMimeService.GetFileMimeType(localGoogleDriveFile.FilePath);
            }
            Dictionary <string, string> properties = new Dictionary <string, string>();

            properties.Add("LocalId", localGoogleDriveFile.LocalId);
            properties.Add("AppVersion", localGoogleDriveFile.AppVersion);
            properties.Add("DisplayName", localGoogleDriveFile.DisplayName);
            fileMetaData.AppProperties = properties;
            return(fileMetaData);
        }