Esempio n. 1
0
        public async Task upload(System.IO.Stream streamFile, string tokenId, string tokenKey, string name, string parent, long?length = null, string[] pathComponents = null, Action <long> onProgress = null)
        {
            var tokenHandler = new MessageTokenHandler(tokenId, tokenKey)
            {
                driveService = this
            };
            var client = new HttpClient(tokenHandler);

            // create path components
            if (pathComponents != null && pathComponents.Length > 0)
            {
                foreach (var cp in pathComponents)
                {
                    parent = await getOrCreateFolder(parent, cp, tokenId, tokenKey);
                }
            }

            var driveFile = new DriveFile()
            {
                Name = name, MimeType = "video/x-matroska", parents = new string[] { parent }
            };
            var response = await client.PostAsJsonAsync("https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable", driveFile, jsonOptions);

            var location        = response.Headers.GetValues("Location").FirstOrDefault();
            var resumableUpload = ResumableUpload.CreateFromUploadUri(new Uri(location), streamFile);

            if (onProgress != null)
            {
                resumableUpload.ProgressChanged += (Google.Apis.Upload.IUploadProgress obj) =>
                                                   onProgress(obj.BytesSent);
            }

            await resumableUpload.UploadAsync();
        }
Esempio n. 2
0
        public async Task <string> getOrCreateFolder(string parent, string name, string tokenId, string tokenKey)
        {
            await _folderCheck.WaitAsync();

            try
            {
                var builder = new UriBuilder("https://www.googleapis.com/drive/v3/files");
                builder.Port = -1;
                var query = HttpUtility.ParseQueryString(builder.Query);
                query["q"]        = $"mimeType = 'application/vnd.google-apps.folder' and '{parent}' in parents and name = '{name.Replace("'", "\\'")}'";
                query["pageSize"] = "1000";
                builder.Query     = query.ToString();
                var tokenHandler = new MessageTokenHandler(tokenId, tokenKey)
                {
                    driveService = this
                };
                var client   = new HttpClient(tokenHandler);
                var response = await client.GetFromJsonAsync <DriveFileResult>(builder.Uri, jsonOptions);

                if (response.Files.Length > 0)
                {
                    return(response.Files[0].Id);
                }
                var driveFile = new DriveFile()
                {
                    Name = name, MimeType = "application/vnd.google-apps.folder", parents = new string[] { parent }
                };
                var responseNew = await client.PostAsJsonAsync("https://www.googleapis.com/drive/v3/files", driveFile, jsonOptions);

                var st = await responseNew.Content.ReadFromJsonAsync <DriveFile>(jsonOptions);

                return(st.Id);
            }
            finally
            {
                _folderCheck.Release();
            }
        }