Пример #1
0
        public static string uploadJson(DriveService service, string file_path, Google.Apis.Drive.v2.Data.File _folder, string file_id = null)
        {
            try
            {
                if (!CheckInternetConnection())
                {
                    return(null);
                }
                var uploadStream = new FileStream(file_path, FileMode.Open, FileAccess.Read);

                // Create or update the JsonTable
                if (file_id == null)
                {
                    // Create it
                    InsertMediaUpload uploadRequest = service.Files.Insert(
                        new Google.Apis.Drive.v2.Data.File
                    {
                        Title   = Path.GetFileName(file_path),
                        Parents = new List <ParentReference>()
                        {
                            new ParentReference()
                            {
                                Id = _folder.Id
                            }
                        }
                    },
                        uploadStream,
                        MIME_JSON);
                    var task = uploadRequest.Upload();
                    uploadStream.Dispose();
                    return(uploadRequest.ResponseBody.Id);
                }
                else
                {
                    // Update it
                    UpdateMediaUpload uploadRequest = service.Files.Update(
                        new Google.Apis.Drive.v2.Data.File
                    {
                        Title   = Path.GetFileName(file_path),
                        Parents = new List <ParentReference>()
                        {
                            new ParentReference()
                            {
                                Id = _folder.Id
                            }
                        }
                    },
                        file_id,
                        uploadStream,
                        MIME_JSON);
                    var task = uploadRequest.Upload();
                    uploadStream.Dispose();
                    return(uploadRequest.FileId);
                }
            }
            catch (WebException e)
            {
                throw new WebException("There are issues with the connection. Maybe try again later. Full error message: " + e.Message);
            }
        }
        private async Task UploadAsync(Video video)
        {
            using (var fs = new FileStream(fileName, FileMode.Open))
            {
                InsertMediaUpload videoInsertRequest = CreateInsertRequest(video, fs);

                await videoInsertRequest.UploadAsync();
            }
        }
        internal void ModifyMediaUpload(InsertMediaUpload upload)
        {
            // Note the use of ArgumentException here, as this will basically be the result of invalid
            // options being passed to a public method.
            if (IfGenerationMatch != null && IfGenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfGenerationMatch)} and {nameof(IfGenerationNotMatch)} in the same options", "options");
            }
            if (IfMetagenerationMatch != null && IfMetagenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfMetagenerationMatch)} and {nameof(IfMetagenerationNotMatch)} in the same options", "options");
            }

            if (ChunkSize != null)
            {
                upload.ChunkSize = ChunkSize.Value;
            }
            if (PredefinedAcl != null)
            {
                upload.PredefinedAcl =
                    GaxPreconditions.CheckEnumValue((PredefinedAclEnum)PredefinedAcl, nameof(PredefinedAcl));
            }
            if (IfGenerationMatch != null)
            {
                upload.IfGenerationMatch = IfGenerationMatch;
            }
            if (IfGenerationNotMatch != null)
            {
                upload.IfGenerationNotMatch = IfGenerationNotMatch;
            }
            if (IfMetagenerationMatch != null)
            {
                upload.IfMetagenerationMatch = IfMetagenerationMatch;
            }
            if (IfMetagenerationNotMatch != null)
            {
                upload.IfMetagenerationNotMatch = IfMetagenerationNotMatch;
            }
            if (Projection != null)
            {
                upload.Projection = GaxPreconditions.CheckEnumValue((ProjectionEnum)Projection, nameof(Projection));
            }
            if (UserProject != null)
            {
                upload.UserProject = UserProject;
            }

            // Note: specifying this and EncryptionKey as non-null/non-None is invalid, but that's checked in StorageClientImpl.
            if (KmsKeyName != null)
            {
                upload.KmsKeyName = KmsKeyName;
            }
        }
        public void ModifyMediaUpload_DefaultOptions()
        {
            var upload  = new InsertMediaUpload(new DummyService(), null, "bucket", new MemoryStream(), null);
            var options = new UploadObjectOptions();

            options.ModifyMediaUpload(upload);
            Assert.Equal(ResumableUpload <InsertMediaUpload> .DefaultChunkSize, upload.ChunkSize);
            Assert.Null(upload.IfGenerationMatch);
            Assert.Null(upload.IfGenerationNotMatch);
            Assert.Null(upload.IfMetagenerationMatch);
            Assert.Null(upload.IfMetagenerationNotMatch);
            Assert.Null(upload.PredefinedAcl);
            Assert.Null(upload.Projection);
        }
        internal void ModifyMediaUpload(InsertMediaUpload upload)
        {
            // Note the use of ArgumentException here, as this will basically be the result of invalid
            // options being passed to a public method.
            if (IfGenerationMatch != null && IfGenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfGenerationMatch)} and {nameof(IfGenerationNotMatch)} in the same options", "options");
            }
            if (IfMetagenerationMatch != null && IfMetagenerationNotMatch != null)
            {
                throw new ArgumentException($"Cannot specify {nameof(IfMetagenerationMatch)} and {nameof(IfMetagenerationNotMatch)} in the same options", "options");
            }

            if (ChunkSize != null)
            {
                upload.ChunkSize = ChunkSize.Value;
            }
            if (PredefinedAcl != null)
            {
                upload.PredefinedAcl =
                    GaxRestPreconditions.CheckEnumValue((PredefinedAclEnum)PredefinedAcl, nameof(PredefinedAcl));
            }
            if (IfGenerationMatch != null)
            {
                upload.IfGenerationMatch = IfGenerationMatch;
            }
            if (IfGenerationNotMatch != null)
            {
                upload.IfGenerationNotMatch = IfGenerationNotMatch;
            }
            if (IfMetagenerationMatch != null)
            {
                upload.IfMetagenerationMatch = IfMetagenerationMatch;
            }
            if (IfMetagenerationNotMatch != null)
            {
                upload.IfMetagenerationNotMatch = IfMetagenerationNotMatch;
            }
            if (Projection != null)
            {
                upload.Projection = GaxRestPreconditions.CheckEnumValue((ProjectionEnum)Projection, nameof(Projection));
            }
        }
Пример #6
0
        public void ModifyMediaUpload_MatchNotMatchConflicts()
        {
            var upload = new InsertMediaUpload(new DummyService(), null, "bucket", new MemoryStream(), null);

            Assert.Throws <ArgumentException>(() =>
            {
                var options = new UploadObjectOptions {
                    IfGenerationMatch = 1L, IfGenerationNotMatch = 2L
                };
                options.ModifyMediaUpload(upload);
            });
            Assert.Throws <ArgumentException>(() =>
            {
                var options = new UploadObjectOptions {
                    IfMetagenerationMatch = 1L, IfMetagenerationNotMatch = 2L
                };
                options.ModifyMediaUpload(upload);
            });
        }
Пример #7
0
        public void ModifyMediaUpload_AllOptions_NegativeMatch()
        {
            var upload  = new InsertMediaUpload(new DummyService(), null, "bucket", new MemoryStream(), null);
            var options = new UploadObjectOptions
            {
                ChunkSize                = UploadObjectOptions.MinimumChunkSize * 3,
                IfGenerationNotMatch     = 1L,
                IfMetagenerationNotMatch = 2L,
                PredefinedAcl            = PredefinedObjectAcl.BucketOwnerRead,
                Projection               = Projection.Full
            };

            options.ModifyMediaUpload(upload);
            Assert.Null(upload.IfGenerationMatch);
            Assert.Equal(1L, upload.IfGenerationNotMatch);
            Assert.Null(upload.IfMetagenerationMatch);
            Assert.Equal(2L, upload.IfMetagenerationNotMatch);
            Assert.Equal(PredefinedAclEnum.BucketOwnerRead, upload.PredefinedAcl);
            Assert.Equal(ProjectionEnum.Full, upload.Projection);
        }
Пример #8
0
        // Upload functions
        public static async void UploadGame(DriveService service, string file_path, Google.Apis.Drive.v2.Data.File _folder, Action <IUploadProgress, BackgroundWorker, string, long> onFileUploadProgressChanged, Action <Google.Apis.Drive.v2.Data.File, BackgroundWorker> onFileUploaded, BackgroundWorker sendingworker, string file_id = null)
        {
            if (!CheckInternetConnection())
            {
                sendingworker.ReportProgress(0, new StatusHolder()
                {
                    txt = "Not connected to the internet. Aborted.", progress = -1
                }); sendingworker.CancelAsync(); return;
            }

            try
            {
                // Reset the WaitHandler
                stopWaitHandle.Reset();

                // Load the file
                var uploadStream = new FileStream(file_path, FileMode.Open, FileAccess.Read);

                // Check if the file should be overwritten or newly created
                if (file_id != null)
                {
                    // Update existing file
                    UpdateMediaUpload uploadRequest = service.Files.Update(
                        new Google.Apis.Drive.v2.Data.File
                    {
                        Title   = Path.GetFileName(file_path),
                        Parents = new List <ParentReference>()
                        {
                            new ParentReference()
                            {
                                Id = _folder.Id
                            }
                        }
                    },
                        file_id,
                        uploadStream,
                        MIME_7ZIP);

                    uploadRequest.ChunkSize = ResumableUpload <InsertRequest> .MinimumChunkSize;

                    uploadRequest.ProgressChanged  += (progress) => onFileUploadProgressChanged(progress, sendingworker, Path.GetFileNameWithoutExtension(file_path), new FileInfo(file_path).Length);
                    uploadRequest.ResponseReceived += (file) => onFileUploaded(file, sendingworker);

                    var task = uploadRequest.UploadAsync();
                    await task.ContinueWith(t =>
                    {
                        uploadStream.Dispose();
                    });
                }
                else
                {
                    // Create new file
                    InsertMediaUpload uploadRequest = service.Files.Insert(
                        new Google.Apis.Drive.v2.Data.File
                    {
                        Title   = Path.GetFileName(file_path),
                        Parents = new List <ParentReference>()
                        {
                            new ParentReference()
                            {
                                Id = _folder.Id
                            }
                        }
                    },
                        uploadStream,
                        MIME_7ZIP);
                    uploadRequest.ChunkSize = ResumableUpload <InsertRequest> .MinimumChunkSize;

                    uploadRequest.ProgressChanged  += (progress) => onFileUploadProgressChanged(progress, sendingworker, Path.GetFileNameWithoutExtension(file_path), new FileInfo(file_path).Length);
                    uploadRequest.ResponseReceived += (file) => onFileUploaded(file, sendingworker);

                    var task = uploadRequest.UploadAsync();
                    await task.ContinueWith(t =>
                    {
                        uploadStream.Dispose();
                    });
                }
                // Wait for whichever comes first: User aborts the task; Upload was successful
                stopWaitHandle.WaitOne();

                // Clear the local fileStream. Note that this will also abort any current upload tasks.
                uploadStream.Dispose();
            }
            catch (WebException e)
            {
                sendingworker.ReportProgress(0, new StatusHolder()
                {
                    txt = "There are issues with the connection. Aborted.", progress = -1
                });
                sendingworker.ReportProgress(0, new StatusHolder()
                {
                    txt = "Error message: " + Environment.NewLine + e.Message, progress = -1
                });
                sendingworker.CancelAsync();
            }
        }