コード例 #1
0
        public void PushbulletPushFileTest()
        {
            try
            {
                var devices = Client.CurrentUsersDevices();
                Assert.IsNotNull(devices);

                var device = devices.Devices.Where(o => o.manufacturer == "Apple").FirstOrDefault();
                Assert.IsNotNull(device, "Could not find the device specified.");

                using (var fileStream = new FileStream(@"c:\daftpunk.png", FileMode.Open, FileAccess.Read, FileShare.Read)) {
                    PushFileRequest request = new PushFileRequest()
                    {
                        device_iden = device.iden,
                        file_name   = "daftpunk.png",
                        file_type   = "image/png",
                        file_stream = fileStream,
                        body        = "Work It Harder\r\nMake It Better\r\nDo It Faster"
                    };

                    var response = Client.PushFile(request);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
コード例 #2
0
ファイル: GrpcCopyClient.cs プロジェクト: uilit/BuildXL
        private async Task StreamContentAsync(Stream input, byte[] buffer, IClientStreamWriter <PushFileRequest> requestStream, CancellationToken ct)
        {
            Contract.Requires(!(input is null));
            Contract.Requires(!(requestStream is null));

            int chunkSize = 0;

            // Pre-fill buffer with the file's first chunk
            await readNextChunk();

            while (true)
            {
                ct.ThrowIfCancellationRequested();

                if (chunkSize == 0)
                {
                    break;
                }

                ByteString content = ByteString.CopyFrom(buffer, 0, chunkSize);
                var        request = new PushFileRequest()
                {
                    Content = content
                };

                // Read the next chunk while waiting for the response
                await Task.WhenAll(readNextChunk(), requestStream.WriteAsync(request));
            }

            async Task <int> readNextChunk()
            {
                chunkSize = await input.ReadAsync(buffer, 0, buffer.Length, ct); return(chunkSize);
            }
        }
コード例 #3
0
        public async void PushBulletPushFileTest()
        {
            try
            {
                var devices = await Client.CurrentUsersDevices();

                Assert.IsNotNull(devices);

                var device = devices.Devices.FirstOrDefault(o => o.Nickname == TestHelper.GetConfig("Device"));
                Assert.IsNotNull(device, "Could not find the device specified.");

                using (var fileStream = new FileStream(@"c:\daftpunk.png", FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    PushFileRequest request = new PushFileRequest()
                    {
                        DeviceIden = device.Iden,
                        FileName   = "daftpunk.png",
                        FileType   = "image/png",
                        FileStream = fileStream,
                        Body       = "Work It Harder\r\nMake It Better\r\nDo It Faster"
                    };

                    var response = await Client.PushFile(request);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
コード例 #4
0
        /// <summary>
        /// Pushes the file.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">file request</exception>
        /// <exception cref="System.Exception">
        /// </exception>
        public PushResponse PushFile(PushFileRequest request)
        {
            try
            {
                #region pre-processing

                if (request == null)
                {
                    throw new ArgumentNullException("file request");
                }

                if (string.IsNullOrWhiteSpace(request.DeviceIden) && string.IsNullOrWhiteSpace(request.Email))
                {
                    throw new Exception(PushbulletConstants.PushRequestErrorMessages.EmptyEmailProperty);
                }

                if (string.IsNullOrWhiteSpace(request.FileName))
                {
                    throw new Exception(PushbulletConstants.PushFileErrorMessages.EmptyFileNameProperty);
                }

                if (string.IsNullOrWhiteSpace(request.FileType))
                {
                    throw new Exception(PushbulletConstants.PushFileErrorMessages.EmptyFileTypeProperty);
                }

                if (request.FileStream == null)
                {
                    throw new Exception(PushbulletConstants.PushFileErrorMessages.EmptyFileStreamProperty);
                }

                if (!request.FileStream.CanRead)
                {
                    throw new Exception(PushbulletConstants.PushFileErrorMessages.CantReadFileStreamProperty);
                }

                #endregion pre-processing


                #region processing

                FileUploadResponse uploadRequestResponse = PostRequest <FileUploadResponse>(string.Concat(PushbulletConstants.BaseUrl, PushbulletConstants.FileUrls.UploadRequest), request);

                if (uploadRequestResponse.Data == null || string.IsNullOrWhiteSpace(uploadRequestResponse.FileUrl))
                {
                    throw new Exception(PushbulletConstants.PushFileErrorMessages.ErrorMakingFileUploadRequest);
                }

                PushFileToAmazonAWS(request, uploadRequestResponse);
                request.FileUrl = uploadRequestResponse.FileUrl;
                return(PostPushRequest <PushFileRequest>(request));

                #endregion processing
            }
            catch (Exception)
            {
                throw;
            }
        }
コード例 #5
0
        /// <summary>
        /// Pushes the file.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">file request</exception>
        public PushResponse PushFile(PushFileRequest request)
        {
            try
            {
                #region pre-processing

                if (request == null)
                {
                    throw new ArgumentNullException("file request");
                }

                if (string.IsNullOrWhiteSpace(request.device_iden) && string.IsNullOrWhiteSpace(request.email))
                {
                    throw new Exception(PushbulletConstants.PushRequestErrorMessages.EmptyEmailProperty);
                }

                if (string.IsNullOrWhiteSpace(request.file_name))
                {
                    throw new Exception(PushbulletConstants.PushFileErrorMessages.EmptyFileNameProperty);
                }

                if (string.IsNullOrWhiteSpace(request.file_type))
                {
                    throw new Exception(PushbulletConstants.PushFileErrorMessages.EmptyFileTypeProperty);
                }

                if (request.file_stream == null)
                {
                    throw new Exception(PushbulletConstants.PushFileErrorMessages.EmptyFileStreamProperty);
                }

                if (!request.file_stream.CanRead)
                {
                    throw new Exception(PushbulletConstants.PushFileErrorMessages.CantReadFileStreamProperty);
                }

                #endregion pre-processing


                #region processing

                FileUploadResponse uploadRequestResponse = PostFileUploadRequest(request);

                if (uploadRequestResponse.data == null || string.IsNullOrWhiteSpace(uploadRequestResponse.file_url))
                {
                    throw new Exception(PushbulletConstants.PushFileErrorMessages.ErrorMakingFileUploadRequest);
                }

                PushFileToAmazonAWS(request, uploadRequestResponse);
                request.file_url = uploadRequestResponse.file_url;
                return(PostPushRequest(JsonSerializer.Serialize(request)));

                #endregion processing
            }
            catch (Exception)
            {
                throw;
            }
        }
コード例 #6
0
        /// <summary>
        /// Posts the file upload request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        private FileUploadResponse PostFileUploadRequest(PushFileRequest request)
        {
            string             requestJson  = JsonSerializer.Serialize(request);
            string             responseJson = PostRequest(string.Concat(PushbulletConstants.BaseUrl, PushbulletConstants.FileUrls.UploadRequest), requestJson);
            FileUploadResponse response     = JsonSerializer.Deserialize <FileUploadResponse>(responseJson);

            return(response);
        }
コード例 #7
0
        /// <summary>
        /// Pushes the file to amazon aws.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="fileUploadResponse">The file upload response.</param>
        /// <exception cref="System.Exception"></exception>
        private void PushFileToAmazonAWS(PushFileRequest request, FileUploadResponse fileUploadResponse)
        {
            StringContent    awsaccesskeyidContent = null;
            StringContent    aclContent            = null;
            StringContent    keyContent            = null;
            StringContent    signatureContent      = null;
            StringContent    policyContent         = null;
            StringContent    contentTypeContent    = null;
            StringContent    cacheControlContent   = null;
            ByteArrayContent fileContent           = null;

            try
            {
                using (var multiPartCont = new MultipartFormDataContent())
                {
                    awsaccesskeyidContent = CreateStringContentFromNameValue(FileUploadResponseData.Properties.AWSAccessKeyId, fileUploadResponse.Data.AWSAccessKeyId);
                    aclContent            = CreateStringContentFromNameValue(FileUploadResponseData.Properties.Acl, fileUploadResponse.Data.Acl);
                    keyContent            = CreateStringContentFromNameValue(FileUploadResponseData.Properties.Key, fileUploadResponse.Data.Key);
                    signatureContent      = CreateStringContentFromNameValue(FileUploadResponseData.Properties.Signature, fileUploadResponse.Data.Signature);
                    policyContent         = CreateStringContentFromNameValue(FileUploadResponseData.Properties.Policy, fileUploadResponse.Data.Policy);
                    contentTypeContent    = CreateStringContentFromNameValue(PushbulletConstants.AmazonHeaders.ContentType, fileUploadResponse.FileType);

                    multiPartCont.Add(awsaccesskeyidContent);
                    multiPartCont.Add(aclContent);
                    multiPartCont.Add(keyContent);
                    multiPartCont.Add(signatureContent);
                    multiPartCont.Add(policyContent);
                    multiPartCont.Add(contentTypeContent);

                    using (var memoryStream = new MemoryStream()) {
                        request.FileStream.CopyTo(memoryStream);
                        fileContent = new ByteArrayContent(memoryStream.ToArray());
                    }

                    fileContent.Headers.Add(PushbulletConstants.AmazonHeaders.ContentType, PushbulletConstants.MimeTypes.OctetStream);
                    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                    {
                        Name     = string.Format("\"{0}\"", "file"),
                        FileName = string.Format("\"{0}\"", request.FileName)
                    };

                    multiPartCont.Add(fileContent);

                    using (var httpClient = new HttpClient())
                    {
                        Task <HttpResponseMessage> httpRequest  = httpClient.PostAsync(fileUploadResponse.UploadUrl, multiPartCont);
                        HttpResponseMessage        httpResponse = httpRequest.Result;

                        Task <string> xmlContentResponse = httpResponse.Content.ReadAsStringAsync();
                        if (!string.IsNullOrWhiteSpace(xmlContentResponse.Result))
                        {
                            throw new Exception(xmlContentResponse.Result);
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (awsaccesskeyidContent != null)
                {
                    awsaccesskeyidContent.Dispose();
                }
                if (aclContent != null)
                {
                    aclContent.Dispose();
                }
                if (keyContent != null)
                {
                    keyContent.Dispose();
                }
                if (signatureContent != null)
                {
                    signatureContent.Dispose();
                }
                if (policyContent != null)
                {
                    policyContent.Dispose();
                }
                if (contentTypeContent != null)
                {
                    contentTypeContent.Dispose();
                }
                if (cacheControlContent != null)
                {
                    cacheControlContent.Dispose();
                }
                if (fileContent != null)
                {
                    fileContent.Dispose();
                }
            }
        }