public BWebServiceResponse(int _StatusCode, BStringOrStream _ResponseContent, string _ResponseContentType = null) { StatusCode = _StatusCode; Headers = new Dictionary <string, IEnumerable <string> >(); ResponseContent = _ResponseContent; ResponseContentType = _ResponseContentType; }
private static void AnalyzeResponse( HttpWebResponse _Response, out bool _bHttpRequestSuccess, out int _HttpRequestResponseCode, out string _HttpRequestResponseContentType, out BStringOrStream _HttpRequestResponseContent, out Dictionary <string, IEnumerable <string> > _HttpRequestResponseHeaders, Action <string> _ErrorMessageAction) { _bHttpRequestSuccess = false; _HttpRequestResponseCode = BWebResponse.Error_InternalError_Code; _HttpRequestResponseContentType = ""; _HttpRequestResponseContent = null; _HttpRequestResponseHeaders = new Dictionary <string, IEnumerable <string> >(); try { _HttpRequestResponseCode = (int)_Response.StatusCode; BWebUtilities.InjectHeadersIntoDictionary(_Response.Headers, _HttpRequestResponseHeaders); _HttpRequestResponseContentType = _Response.ContentType; using (var ResStream = _Response.GetResponseStream()) { var CopyStream = new BMemoryTributary(BUtility.ReadToEnd(ResStream)); _HttpRequestResponseContent = new BStringOrStream(CopyStream, CopyStream.Length, () => { try { CopyStream?.Dispose(); } catch { } }); } _bHttpRequestSuccess = true; } catch (Exception e) { _ErrorMessageAction?.Invoke("Error: RequestRedirection-AnalyzeResponse: " + e.Message + ", Trace: " + e.StackTrace); _bHttpRequestSuccess = false; } }
/// <summary> /// /// <para>UploadFile:</para> /// /// <para>Uploads a local file to File Service, caller thread will be blocked before it is done</para> /// /// <para>EBRemoteFileReadPublicity does nothing as Azure does not support per object authentication, only per container</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.UploadFile"/> for detailed documentation</para> /// /// </summary> public bool UploadFile(BStringOrStream _LocalFileOrStream, string _BucketName, string _KeyInBucket, EBRemoteFileReadPublicity _RemoteFileReadAccess = EBRemoteFileReadPublicity.AuthenticatedRead, Tuple <string, string>[] _FileTags = null, Action <string> _ErrorMessageAction = null) { if (AServiceClient == null) { _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: GSClient is null."); return(false); } BlobContainerClient ContainerClient = AServiceClient.GetBlobContainerClient(_BucketName); BlobClient Blob = ContainerClient.GetBlobClient(_KeyInBucket); Response <BlobContentInfo> Response = null; if (_LocalFileOrStream.Type == EBStringOrStreamEnum.String) { if (!BUtility.DoesFileExist( _LocalFileOrStream.String, out bool bLocalFileExists, _ErrorMessageAction)) { _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: DoesFileExist failed."); return(false); } if (!bLocalFileExists) { _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: Local file does not exist."); return(false); } using (FileStream FS = new FileStream(_LocalFileOrStream.String, FileMode.Open, FileAccess.Read)) { try { Response = Blob.Upload(FS); if (Response.Value == null) { _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: Operation has failed."); return(false); } } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } } } else { try { Response = Blob.Upload(_LocalFileOrStream.Stream); if (Response.Value == null) { _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: Operation has failed."); return(false); } } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } } if (Response != null) { if (_FileTags != null && _FileTags.Length > 0) { Dictionary <string, string> NewMetadata = new Dictionary <string, string>(); foreach (Tuple <string, string> CurrentTag in _FileTags) { NewMetadata.Add(CurrentTag.Item1, CurrentTag.Item2); } try { Blob.SetMetadata(NewMetadata); } catch (Exception ex) { _ErrorMessageAction?.Invoke($"BFileServiceAZ->UploadFile: {ex.Message}, Trace: {ex.StackTrace}"); return(false); } } var FileName = _KeyInBucket; var FileNameIndex = _KeyInBucket.LastIndexOf("/") + 1; if (FileNameIndex > 0) { FileName = _KeyInBucket.Substring(FileNameIndex, _KeyInBucket.Length - FileNameIndex); } BlobHttpHeaders Header = new BlobHttpHeaders() { ContentDisposition = $"inline; filename={FileName}" }; try { Blob.SetHttpHeaders(Header); } catch (Exception ex) { _ErrorMessageAction?.Invoke($"BFileServiceAZ->UploadFile: {ex.Message}, Trace: {ex.StackTrace}"); return(false); } } return(true); }
/// <summary> /// /// <para>DownloadFile:</para> /// /// <para>Downloads a file from File Service and stores locally/or to stream, caller thread will be blocked before it is done</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.DownloadFile"/> for detailed documentation</para> /// /// </summary> public bool DownloadFile(string _BucketName, string _KeyInBucket, BStringOrStream _Destination, Action <string> _ErrorMessageAction = null, ulong _StartIndex = 0, ulong _Size = 0) { BlobContainerClient ContainerClient = AServiceClient.GetBlobContainerClient(_BucketName); BlobClient Blob = ContainerClient.GetBlobClient(_KeyInBucket); Response <BlobProperties> Response = Blob.GetProperties(); if (AServiceClient == null) { _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: AServiceClient is null."); return(false); } if (!CheckFileExistence( _BucketName, _KeyInBucket, out bool bExists, _ErrorMessageAction )) { _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: CheckFileExistence failed."); return(false); } if (!bExists) { _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: File does not exist in the File Service."); return(false); } HttpRange Range = default(HttpRange); if (_Size > 0) { Range = new HttpRange((long)_StartIndex, (long)(_StartIndex + _Size)); } try { if (_Destination.Type == EBStringOrStreamEnum.String) { using (FileStream FS = File.Create(_Destination.String)) { BlobDownloadInfo DlInfo = Blob.Download(Range).Value; DlInfo.Content.CopyTo(FS); DlInfo.Dispose(); } if (!BUtility.DoesFileExist( _Destination.String, out bool bLocalFileExists, _ErrorMessageAction)) { _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: DoesFileExist failed."); return(false); } if (!bLocalFileExists) { _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: Download finished, but still file does not locally exist."); return(false); } } else { if (_Destination.Stream == null) { _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: Destination stream is null."); return(false); } BlobDownloadInfo DlInfo = Blob.Download(Range).Value; DlInfo.Content.CopyTo(_Destination.Stream); DlInfo.Dispose(); try { _Destination.Stream.Position = 0; } catch (Exception) { } } } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } return(true); }
private static bool GetProcessedFileNode_Internal( ENodeType _FileType, IBDatabaseServiceInterface _DatabaseService, IBFileServiceInterface _FileService, string _CadFileStorageBucketName, string _ModelID, int _RevisionIndex, bool _bRootNodeRequested, uint _NodeStartIndex, uint _NodeSize, out BWebServiceResponse _SuccessResponse, out BWebServiceResponse _FailureResponse, Action <string> _ErrorMessageAction = null) { _SuccessResponse = BWebResponse.InternalError(""); if (!TryGettingAllInfo( _DatabaseService, _ModelID, _RevisionIndex, out ModelDBEntry ModelObject, out Revision RevisionObject, out int _, out _FailureResponse, _ErrorMessageAction)) { return(false); } if (RevisionObject.FileEntry.FileProcessStage != (int)Constants.EProcessStage.Uploaded_Processed) { _FailureResponse = BWebResponse.NotFound("Raw file has not been processed yet."); return(false); } if (_bRootNodeRequested) { Convert.UniqueIDToStartIndexAndSize(RevisionObject.FileEntry.ProcessedFilesRootNodeID, out _NodeStartIndex, out _NodeSize); if (_NodeStartIndex == 0 || _NodeSize == 0) { _FailureResponse = BWebResponse.InternalError("Invalid Root Node ID."); return(false); } } string RelativeFileUrl = null; switch (_FileType) { case ENodeType.Hierarchy: RelativeFileUrl = RevisionObject.FileEntry.HierarchyRAFRelativeUrl; break; case ENodeType.Geometry: RelativeFileUrl = RevisionObject.FileEntry.GeometryRAFRelativeUrl; break; case ENodeType.Metadata: RelativeFileUrl = RevisionObject.FileEntry.MetadataRAFRelativeUrl; break; } Node RetrievedNode; var Buffer = new byte[_NodeSize]; try { using (var MemStream = new MemoryStream((int)_NodeStartIndex)) { var Destination = new BStringOrStream(MemStream, _NodeSize); if (!_FileService.DownloadFile(_CadFileStorageBucketName, RelativeFileUrl, Destination, _ErrorMessageAction, _NodeStartIndex, _NodeSize)) { _ErrorMessageAction?.Invoke("DownloadFile has failed in GetProcessedFileNode_Internal. ModelID: " + _ModelID + ", RevisionIndex: " + _RevisionIndex + ", NodeStartIndex: " + _NodeStartIndex + ", NodeSize: " + _NodeSize); _FailureResponse = BWebResponse.NotFound("Given Node ID does not exist."); return(false); } MemStream.Seek(0, SeekOrigin.Begin); if (MemStream.Read(Buffer, 0, (int)_NodeSize) < (int)_NodeSize) { _FailureResponse = BWebResponse.InternalError("Stream read operation has failed."); return(false); } Convert.BufferToNode(out RetrievedNode, _FileType, Buffer, 0); } } catch (Exception e) { _ErrorMessageAction?.Invoke("File random access/stream operations have failed. ModelID: " + _ModelID + ", RevisionIndex: " + _RevisionIndex + ", NodeStartIndex: " + _NodeStartIndex + ", NodeSize: " + _NodeSize + ", Message: " + e.Message + ", Trace: " + e.StackTrace); _FailureResponse = BWebResponse.NotFound("Given Node ID does not exist."); return(false); } if (RetrievedNode == null) { _FailureResponse = BWebResponse.InternalError("File node parse operation has failed."); return(false); } _SuccessResponse = BWebResponse.StatusOK("Node has been located.", new JObject() { ["node"] = JObject.Parse(JsonConvert.SerializeObject(RetrievedNode)) }); return(true); }
public static InterServicesRequestResponse InterServicesRequest( InterServicesRequestRequest _Request, bool _bKillProcessOnAddAccessTokenForServiceExecutionFailure = true, Action <string> _ErrorMessageAction = null) { var bHttpRequestSuccess = false; var HttpRequestResponseCode = BWebResponse.Error_InternalError_Code; var HttpRequestResponseContentType = ""; BStringOrStream HttpRequestResponseContent = null; Dictionary <string, IEnumerable <string> > HttpRequestResponseHeaders = null; var Request = (HttpWebRequest)WebRequest.Create(_Request.DestinationServiceUrl); Request.Method = _Request.RequestMethod; Request.ServerCertificateValidationCallback = (a, b, c, d) => true; Request.AllowAutoRedirect = false; if (_Request.bWithAuthToken) { //If context-headers already contain authorization; we must rename it to client-authorization to prevent override. if (_Request.UseContextHeaders != null && BWebUtilities.DoesContextContainHeader(out List <string> AuthorizationHeaderValues, out string CaseSensitive_FoundHeaderKey, _Request.UseContextHeaders, "authorization") && BUtility.CheckAndGetFirstStringFromList(AuthorizationHeaderValues, out string ClientAuthorization)) { _Request.UseContextHeaders.Request.Headers.Remove(CaseSensitive_FoundHeaderKey); _Request.UseContextHeaders.Request.Headers.Add("client-authorization", ClientAuthorization); } } var ExcludeHeaderKeysForRequest = LowerContentOfStrings(_Request.ExcludeHeaderKeysForRequest); if (_Request.UseContextHeaders != null) { InsertHeadersFromContextInto(_Request.UseContextHeaders, (string _Key, string _Value) => { if (ExcludeHeaderKeysForRequest != null && ExcludeHeaderKeysForRequest.Contains(_Key.ToLower())) { return; } Request.Headers.Add(_Key, _Value); }); } if (_Request.Headers != null) { InsertHeadersFromDictionaryInto(_Request.Headers, (string _Key, string _Value) => { if (ExcludeHeaderKeysForRequest != null && ExcludeHeaderKeysForRequest.Contains(_Key.ToLower())) { return; } Request.Headers.Add(_Key, _Value); }); } try { if (_Request.RequestMethod != "GET" && /*&& _Request.RequestMethod != "DELETE"*/ _Request.Content != null && ((_Request.Content.Type == EBStringOrStreamEnum.Stream && _Request.Content.Stream != null) || (_Request.Content.Type == EBStringOrStreamEnum.String && _Request.Content.String != null && _Request.Content.String.Length > 0))) { Request.ContentType = _Request.ContentType; using (var OStream = Request.GetRequestStream()) { if (_Request.Content.Type == EBStringOrStreamEnum.Stream) { _Request.Content.Stream.CopyTo(OStream); } else { using (var RStream = new StreamWriter(OStream)) { RStream.Write(_Request.Content.String); } } } } try { using (var Response = (HttpWebResponse)Request.GetResponse()) { AnalyzeResponse(Response, out bHttpRequestSuccess, out HttpRequestResponseCode, out HttpRequestResponseContentType, out HttpRequestResponseContent, out HttpRequestResponseHeaders, _ErrorMessageAction); } } catch (Exception e) { if (e is WebException) { using (var ErrorResponse = (HttpWebResponse)(e as WebException).Response) { AnalyzeResponse(ErrorResponse, out bHttpRequestSuccess, out HttpRequestResponseCode, out HttpRequestResponseContentType, out HttpRequestResponseContent, out HttpRequestResponseHeaders, _ErrorMessageAction); } } else { _ErrorMessageAction?.Invoke("Error: InterServicesRequest: " + e.Message + ", Trace: " + e.StackTrace); bHttpRequestSuccess = false; } } } catch (Exception e) { _ErrorMessageAction?.Invoke("Error: InterServicesRequest: " + e.Message + ", Trace: " + e.StackTrace); bHttpRequestSuccess = false; } if (!bHttpRequestSuccess) { _ErrorMessageAction?.Invoke("Error: Request has failed due to an internal api gateway error. Service endpoint: " + _Request.DestinationServiceUrl); return(InterServicesRequestResponse.InternalErrorOccured("Request has failed due to an internal api gateway error.")); } if (_Request.UseContextHeaders != null) { InsertHeadersFromDictionaryIntoContext(HttpRequestResponseHeaders, _Request.UseContextHeaders); } return(new InterServicesRequestResponse() { bSuccess = true, ResponseCode = HttpRequestResponseCode, ContentType = HttpRequestResponseContentType, ResponseHeaders = HttpRequestResponseHeaders, Content = HttpRequestResponseContent }); }
/// <summary> /// /// <para>UploadFile:</para> /// /// <para>Uploads a local file to File Service, caller thread will be blocked before it is done</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.UploadFile"/> for detailed documentation</para> /// /// </summary> public bool UploadFile( BStringOrStream _LocalFileOrStream, string _BucketName, string _KeyInBucket, EBRemoteFileReadPublicity _RemoteFileReadAccess = EBRemoteFileReadPublicity.AuthenticatedRead, Tuple <string, string>[] _FileTags = null, Action <string> _ErrorMessageAction = null) { if (TransferUtil == null) { _ErrorMessageAction?.Invoke("BFileServiceAWS->UploadFile: TransferUtil is null."); return(false); } TransferUtilityUploadRequest UploadRequest = new TransferUtilityUploadRequest { BucketName = _BucketName, PartSize = 16, Key = _KeyInBucket }; if (_LocalFileOrStream.Type == EBStringOrStreamEnum.String) { if (!BUtility.DoesFileExist( _LocalFileOrStream.String, out bool bLocalFileExists, _ErrorMessageAction)) { _ErrorMessageAction?.Invoke("BFileServiceAWS->UploadFile: DoesFileExist failed."); return(false); } if (!bLocalFileExists) { _ErrorMessageAction?.Invoke("BFileServiceAWS->UploadFile: Local file does not exist."); return(false); } UploadRequest.FilePath = _LocalFileOrStream.String; } else { UploadRequest.InputStream = _LocalFileOrStream.Stream; } if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.PublicRead) { UploadRequest.CannedACL = S3CannedACL.PublicRead; } else if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.ProjectWideProtectedRead) { UploadRequest.CannedACL = S3CannedACL.AuthenticatedRead; } else { UploadRequest.CannedACL = S3CannedACL.AuthenticatedRead; } if (_FileTags != null && _FileTags.Length > 0) { List <Tag> FileTags = new List <Tag>(); foreach (Tuple <string, string> CurrentTag in _FileTags) { Tag NewTag = new Tag { Key = CurrentTag.Item1, Value = CurrentTag.Item2 }; FileTags.Add(NewTag); } UploadRequest.TagSet = FileTags; } try { TransferUtil.Upload(UploadRequest); } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceAWS->UploadFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } return(true); }
/// <summary> /// /// <para>DownloadFile:</para> /// /// <para>Downloads a file from File Service and stores locally/or to stream, caller thread will be blocked before it is done</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.DownloadFile"/> for detailed documentation</para> /// /// </summary> public bool DownloadFile( string _BucketName, string _KeyInBucket, BStringOrStream _Destination, Action <string> _ErrorMessageAction = null, UInt64 _StartIndex = 0, UInt64 _Size = 0) { //TODO: StartIndex and Size implementation if (_Destination == null) { _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: _Destination is null."); return(false); } if (!CheckFileExistence( _BucketName, _KeyInBucket, out bool bExists, _ErrorMessageAction )) { _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: CheckFileExistence failed."); return(false); } if (!bExists) { _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: File does not exist in the File Service."); return(false); } if (_Destination.Type == EBStringOrStreamEnum.String) { if (TransferUtil == null) { _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: TransferUtil is null."); return(false); } try { TransferUtil.Download( _Destination.String, _BucketName, _KeyInBucket ); } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } if (!BUtility.DoesFileExist( _Destination.String, out bool bLocalFileExists, _ErrorMessageAction)) { _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: DoesFileExist failed."); return(false); } if (!bLocalFileExists) { _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: Download finished, but still file does not locally exist."); return(false); } } else { if (S3Client == null) { _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: S3Client is null."); return(false); } if (_Destination.Stream == null) { _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: Destination stream is null."); return(false); } GetObjectRequest GetRequest = new GetObjectRequest { BucketName = _BucketName, Key = _KeyInBucket }; try { using (var CreatedTask = S3Client.GetObjectAsync(GetRequest)) { CreatedTask.Wait(); using (GetObjectResponse GetResponse = CreatedTask.Result) { try { GetResponse.ResponseStream.CopyTo(_Destination.Stream); } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } } } _Destination.Stream.Position = 0; } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: " + e.Message + ", Trace: " + e.StackTrace); } } return(true); }
/// <summary> /// /// <para>UploadFile:</para> /// /// <para>Uploads a local file to File Service, caller thread will be blocked before it is done</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.UploadFile"/> for detailed documentation</para> /// /// </summary> public bool UploadFile( BStringOrStream _LocalFileOrStream, string _BucketName, string _KeyInBucket, EBRemoteFileReadPublicity _RemoteFileReadAccess = EBRemoteFileReadPublicity.AuthenticatedRead, Tuple <string, string>[] _FileTags = null, Action <string> _ErrorMessageAction = null) { if (GSClient == null) { _ErrorMessageAction?.Invoke("BFileServiceGC->UploadFile: GSClient is null."); return(false); } PredefinedObjectAcl CloudTypePublicity; if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.PublicRead) { CloudTypePublicity = PredefinedObjectAcl.PublicRead; } else if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.ProjectWideProtectedRead) { CloudTypePublicity = PredefinedObjectAcl.ProjectPrivate; } else { CloudTypePublicity = PredefinedObjectAcl.AuthenticatedRead; } Google.Apis.Storage.v1.Data.Object UploadedObject = null; if (_LocalFileOrStream.Type == EBStringOrStreamEnum.String) { if (!BUtility.DoesFileExist( _LocalFileOrStream.String, out bool bLocalFileExists, _ErrorMessageAction)) { _ErrorMessageAction?.Invoke("BFileServiceGC->UploadFile: DoesFileExist failed."); return(false); } if (!bLocalFileExists) { _ErrorMessageAction?.Invoke("BFileServiceGC->UploadFile: Local file does not exist."); return(false); } using (FileStream FS = new FileStream(_LocalFileOrStream.String, FileMode.Open, FileAccess.Read)) { try { UploadedObject = GSClient.UploadObject(_BucketName, _KeyInBucket, null, FS, new UploadObjectOptions { PredefinedAcl = CloudTypePublicity }); if (UploadedObject == null) { _ErrorMessageAction?.Invoke("BFileServiceGC->UploadFile: Operation has failed."); return(false); } } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceGC->UploadFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } } } else { try { UploadedObject = GSClient.UploadObject(_BucketName, _KeyInBucket, null, _LocalFileOrStream.Stream, new UploadObjectOptions { PredefinedAcl = CloudTypePublicity }); if (UploadedObject == null) { _ErrorMessageAction?.Invoke("BFileServiceGC->UploadFile: Operation has failed."); return(false); } } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceGC->UploadFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } } if (UploadedObject != null) { UploadedObject.CacheControl = CloudTypePublicity == PredefinedObjectAcl.PublicRead ? "public" : "private"; if (_FileTags != null && _FileTags.Length > 0) { Dictionary <string, string> NewMetadata = new Dictionary <string, string>(); foreach (Tuple <string, string> CurrentTag in _FileTags) { NewMetadata.Add(CurrentTag.Item1, CurrentTag.Item2); } UploadedObject.Metadata = NewMetadata; } var FileName = _KeyInBucket; var FileNameIndex = _KeyInBucket.LastIndexOf("/") + 1; if (FileNameIndex > 0) { FileName = _KeyInBucket.Substring(FileNameIndex, _KeyInBucket.Length - FileNameIndex); } UploadedObject.ContentDisposition = "inline; filename=" + FileName; GSClient.PatchObject(UploadedObject, null); } return(true); }
/// <summary> /// /// <para>DownloadFile:</para> /// /// <para>Downloads a file from File Service and stores locally/or to stream, caller thread will be blocked before it is done</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.DownloadFile"/> for detailed documentation</para> /// /// </summary> public bool DownloadFile( string _BucketName, string _KeyInBucket, BStringOrStream _Destination, Action <string> _ErrorMessageAction = null, UInt64 _StartIndex = 0, UInt64 _Size = 0) { if (GSClient == null) { _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: GSClient is null."); return(false); } if (!CheckFileExistence( _BucketName, _KeyInBucket, out bool bExists, _ErrorMessageAction )) { _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: CheckFileExistence failed."); return(false); } if (!bExists) { _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: File does not exist in the File Service."); return(false); } DownloadObjectOptions DOO = null; if (_Size > 0) { DOO = new DownloadObjectOptions() { Range = new System.Net.Http.Headers.RangeHeaderValue((long)_StartIndex, (long)(_StartIndex + _Size)) }; } try { if (_Destination.Type == EBStringOrStreamEnum.String) { using (FileStream FS = File.Create(_Destination.String)) { GSClient.DownloadObject(_BucketName, _KeyInBucket, FS, DOO); } if (!BUtility.DoesFileExist( _Destination.String, out bool bLocalFileExists, _ErrorMessageAction)) { _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: DoesFileExist failed."); return(false); } if (!bLocalFileExists) { _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: Download finished, but still file does not locally exist."); return(false); } } else { if (_Destination.Stream == null) { _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: Destination stream is null."); return(false); } GSClient.DownloadObject(_BucketName, _KeyInBucket, _Destination.Stream, DOO); try { _Destination.Stream.Position = 0; } catch (Exception) { } } } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } return(true); }