Example #1
0
File: Utils.cs Project: festigf/g3
        /// <summary>
        /// Update both metadata and content of a file and return the updated file.
        /// </summary>
        public static Google.Apis.Drive.v2.Data.File UpdateResource(Google.Apis.Drive.v2.DriveService service, IAuthenticator auth, String fileId, String newTitle,
            String newDescription, String newMimeType, String content, bool newRevision)
        {
            // First retrieve the file from the API.
            Google.Apis.Drive.v2.Data.File body = service.Files.Get(fileId).Fetch();

            body.Title = newTitle;
            body.Description = newDescription;
            body.MimeType = newMimeType;

            byte[] byteArray = Encoding.UTF8.GetBytes(content);
            MemoryStream stream = new MemoryStream(byteArray);

            Google.Apis.Drive.v2.FilesResource.UpdateMediaUpload request = service.Files.Update(body, fileId, stream, newMimeType);
            request.Upload();

            return request.ResponseBody;
        }
Example #2
0
        public void WriteLogMessage(Google.Apis.Upload.IUploadProgress obj)
        {
            bool bIsLast = (Google.Apis.Upload.UploadStatus.Completed == obj.Status);
            Int64 sizeLoaded = obj.BytesSent;
            DateTime timeNow = DateTime.Now;

            if (sizeLoaded > _sizeMax)
            {
                _sizeMax <<= 1;
                bIsLast = true;
                if (_lastProgress > 1)
                {
                    _lastProgress--;
                    _deltaProgress--;
                }
            }

            if (Google.Apis.Upload.UploadStatus.Starting == obj.Status)
            {
                _startUpdate = _lastUpdate = timeNow;
                _sizeMax = 256 * 1024 * 1024;
            }
            else if (bIsLast || Google.Apis.Upload.UploadStatus.Uploading == obj.Status)
            {
                int seconds = (int)Math.Max(0, (timeNow - _startUpdate).TotalSeconds);
                ushort uProgress = (ushort)((sizeLoaded * 100) / _sizeMax);
                bool bNextProgress = (bIsLast || uProgress > _lastProgress);
                ulong mseconds = Math.Max(1, (ulong)(timeNow - ((bNextProgress) ? _startUpdate : _lastUpdate)).TotalMilliseconds);
                ulong deltaBytes = (ulong)((bNextProgress) ? sizeLoaded : (sizeLoaded - _lastSentBytes));

                string message = string.Format(
                                        "{0} {1}> Upload {2} at {3} Kb/s, progress: {4}%",
                                        timeNow.ToLocalTime().ToShortDateString(),
                                        timeNow.ToLocalTime().ToShortTimeString(),
                                        GetSizeString(sizeLoaded),
                                        ((deltaBytes >> 10) * 1000) / mseconds,
                                        uProgress
                                        ).PadRight(75);

                if (bIsLast || (uProgress < 100 && uProgress > _lastProgress))
                {
                    Console.WriteLine(message);
                    _lastProgress = (ushort)(uProgress + _deltaProgress);
                }
                else if (!_bIsFileLog)
                {
                    Console.Write(message);
                    Console.CursorLeft = 0;
                }

                _lastUpdate = timeNow;
                _lastSentBytes = sizeLoaded;
            }
        }
Example #3
0
File: Utils.cs Project: festigf/g3
        /// <summary>
        /// Create a new file and return it.
        /// </summary>
        public static Google.Apis.Drive.v2.Data.File InsertResource(Google.Apis.Drive.v2.DriveService service, IAuthenticator auth, String title,
            String description, String mimeType, String content)
        {
            // File's metadata.
            Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
            body.Title = title;
            body.Description = description;
            body.MimeType = mimeType;

            byte[] byteArray = Encoding.ASCII.GetBytes(content);
            MemoryStream stream = new MemoryStream(byteArray);

            Google.Apis.Drive.v2.FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
            request.Upload();

            return request.ResponseBody;
        }
Example #4
0
 private static void ProgressChanged(Google.Apis.Upload.IUploadProgress obj)
 {
     Console.WriteLine("Bytes sent: {0}, Status: {1}, Exception: {2}", obj.BytesSent, obj.Status, obj.Exception);
 }