示例#1
0
        private void ProcessFileDownloadRequest(Dictionary <string, object> externalQueryParam, PNCallback <PNDownloadFileResult> callback)
        {
            IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(config, jsonLibrary, unit, pubnubLog, pubnubTelemetryMgr, (PubnubInstance != null) ? PubnubInstance.InstanceId : "");

            Uri request = urlBuilder.BuildGetFileUrlOrDeleteReqest("GET", "", this.channelName, this.currentFileId, this.currentFileName, externalQueryParam, PNOperationType.PNDownloadFileOperation);

            RequestState <PNDownloadFileResult> requestState = new RequestState <PNDownloadFileResult>();

            requestState.ResponseType      = PNOperationType.PNDownloadFileOperation;
            requestState.PubnubCallback    = callback;
            requestState.Reconnect         = false;
            requestState.EndPointOperation = this;

            byte[] item1Bytes = null;
            UrlProcessRequestForStream(request, requestState, false, "").ContinueWith(r =>
            {
                item1Bytes = r.Result.Item1;
                if (item1Bytes != null)
                {
                    byte[] outputBytes      = null;
                    string currentCipherKey = !string.IsNullOrEmpty(this.currentFileCipherKey) ? this.currentFileCipherKey : config.CipherKey;
                    if (currentCipherKey.Length > 0)
                    {
                        try
                        {
                            PubnubCrypto aes = new PubnubCrypto(currentCipherKey, config, pubnubLog, null);
                            outputBytes      = aes.Decrypt(item1Bytes, true);
                            LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0}, Stream length (after Decrypt)= {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), item1Bytes.Length), config.LogVerbosity);
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.ToString());
                        }
                    }
                    else
                    {
                        outputBytes = item1Bytes;
                    }
                    PNDownloadFileResult result = new PNDownloadFileResult();
                    result.FileBytes            = outputBytes;
                    result.FileName             = currentFileName;
                    callback.OnResponse(result, r.Result.Item2);
                }
                else
                {
                    if (r.Result.Item2 != null)
                    {
                        callback.OnResponse(null, r.Result.Item2);
                    }
                }
            }, TaskContinuationOptions.ExecuteSynchronously).Wait();
        }
        public void Async(Action <PNDownloadFileResult, PNStatus> callback)
        {
            PNDownloadFileResult pnDownloadFileResult = new PNDownloadFileResult();
            string cipherKeyToUse = "";
            string dlFilePath     = DownloadFileSavePath;
            string tempFilePath   = string.Format("{0}/{1}.dl.enc", Application.temporaryCachePath, DownloadFileName);

            if (!string.IsNullOrEmpty(DownloadFileCipherKey))
            {
                cipherKeyToUse = DownloadFileCipherKey;
                dlFilePath     = tempFilePath;
            }
            else if (!string.IsNullOrEmpty(PubNubInstance.PNConfig.CipherKey))
            {
                cipherKeyToUse = PubNubInstance.PNConfig.CipherKey;
                dlFilePath     = tempFilePath;
            }
            s3.Channel(DownloadFileChannel).ID(DownloadFileID).Name(DownloadFileName).SavePath(dlFilePath);
            s3.ExecuteDownloadFile((status) => {
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("request.GetResponseHeader {0}", status.StatusCode), PNLoggingMethod.LevelInfo);
                #endif
                if (status.StatusCode != 200)
                {
                    pnDownloadFileResult = null;
                    callback(pnDownloadFileResult, status);
                }
                else
                {
                    // Decrypt
                    // save file to temp, decrypt, save file to desired location
                    if (!string.IsNullOrEmpty(cipherKeyToUse))
                    {
                        PubnubCrypto pubnubCrypto = new PubnubCrypto(cipherKeyToUse, this.PubNubInstance.PNLog);
                        pubnubCrypto.DecryptFile(dlFilePath, DownloadFileSavePath);
                    }
                    #if (ENABLE_PUBNUB_LOGGING)
                    this.PubNubInstance.PNLog.WriteToLog(string.Format("File successfully downloaded and saved to  {0}", DownloadFileSavePath), PNLoggingMethod.LevelInfo);
                    #endif
                    callback(pnDownloadFileResult, status);
                }
            });
        }
示例#3
0
        private async Task <PNResult <PNDownloadFileResult> > ProcessFileDownloadRequest(Dictionary <string, object> externalQueryParam)
        {
            PNResult <PNDownloadFileResult> ret = new PNResult <PNDownloadFileResult>();

            if (string.IsNullOrEmpty(this.currentFileId))
            {
                PNStatus errStatus = new PNStatus {
                    Error = true, ErrorData = new PNErrorData("Missing File Id", new ArgumentException("Missing File Id"))
                };
                ret.Status = errStatus;
                return(ret);
            }

            if (string.IsNullOrEmpty(this.currentFileName))
            {
                PNStatus errStatus = new PNStatus {
                    Error = true, ErrorData = new PNErrorData("Invalid file name", new ArgumentException("Invalid file name"))
                };
                ret.Status = errStatus;
                return(ret);
            }


            IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(config, jsonLibrary, unit, pubnubLog, pubnubTelemetryMgr, (PubnubInstance != null) ? PubnubInstance.InstanceId : "");

            Uri request = urlBuilder.BuildGetFileUrlOrDeleteReqest("GET", "", this.channelName, this.currentFileId, this.currentFileName, externalQueryParam, PNOperationType.PNDownloadFileOperation);

            RequestState <PNDownloadFileResult> requestState = new RequestState <PNDownloadFileResult>();

            requestState.ResponseType      = PNOperationType.PNDownloadFileOperation;
            requestState.Reconnect         = false;
            requestState.EndPointOperation = this;

            Tuple <byte[], PNStatus> JsonAndStatusTuple = await UrlProcessRequestForStream(request, requestState, false, "").ConfigureAwait(false);

            ret.Status = JsonAndStatusTuple.Item2;
            byte[] item1Bytes = JsonAndStatusTuple.Item1;
            if (item1Bytes != null)
            {
                byte[] outputBytes      = null;
                string currentCipherKey = !string.IsNullOrEmpty(this.currentFileCipherKey) ? this.currentFileCipherKey : config.CipherKey;
                if (currentCipherKey.Length > 0)
                {
                    try
                    {
                        PubnubCrypto aes = new PubnubCrypto(currentCipherKey, config, pubnubLog, null);
                        outputBytes = aes.Decrypt(item1Bytes, true);
                        LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0}, Stream length (after Decrypt)= {1}", DateTime.Now.ToString(CultureInfo.InvariantCulture), item1Bytes.Length), config.LogVerbosity);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
                    }
                }
                else
                {
                    outputBytes = item1Bytes;
                }

                PNDownloadFileResult result = new PNDownloadFileResult();
                result.FileBytes = outputBytes;
                result.FileName  = currentFileName;
                ret.Result       = result;
            }

            return(ret);
        }