/// <summary> /// Updates the Resource.File of the specified resource. /// </summary> /// <param name="collectionName">The type of the resource.</param> /// <param name="memberResourceId">The resource whose File needs to be updated.</param> /// <param name="mimeType">The MIME type of media.</param> /// <param name="media">The new File contents.</param> /// <returns>A SyndicationItem that describes the updated resource.</returns> /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty /// or media is null.</exception> /// <exception cref="ArgumentException">Throws exception if requested memberResourceId is not a unique identifier.</exception> SyndicationItem IAtomPubStoreWriter.UpdateMedia(string collectionName, string memberResourceId, string mimeType, byte[] media) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (null == media) { throw new ArgumentNullException("media"); } if (string.IsNullOrEmpty(memberResourceId)) { throw new ArgumentNullException("memberResourceId"); } if (!AtomPubHelper.IsValidGuid(memberResourceId)) { throw new ArgumentException(Resources.ATOMPUB_INVALID_RESOURCE_ID, "memberResourceId"); } using (ZentityContext context = CoreHelper.CreateZentityContext()) { Type collectionType = CoreHelper.GetSystemResourceType(collectionName); // Prepare a query to get a resource with specified Id and specified type. string commandText = string.Format(CultureInfo.InvariantCulture, AtomPubConstants.EsqlToGetFileContents, collectionType.FullName); ObjectQuery <Core.File> query = new ObjectQuery <Core.File>(commandText, context); query.Parameters.Add(new ObjectParameter("Id", new Guid(memberResourceId))); Core.File mediaResource = query.FirstOrDefault(); if (null == mediaResource) { throw new ResourceNotFoundException(Resources.ATOMPUB_RESOURCE_NOT_FOUND); } if (!mediaResource.Authorize("Update", context, CoreHelper.GetAuthenticationToken())) { throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED); } mediaResource.Resources.Load(); ScholarlyWork resource = (ScholarlyWork)mediaResource.Resources.First(); resource.DateModified = DateTime.Now; mediaResource.MimeType = mimeType; mediaResource.FileExtension = AtomPubHelper.GetFileExtension(mimeType); MemoryStream mediaStream = ZentityAtomPubStoreWriter.GetMediaStream(media); context.UploadFileContent(mediaResource, mediaStream); // Bug Fix : 180811 - Save Changes once mime type and contents are set. context.SaveChanges(); return(ZentityAtomPubStoreReader.GenerateSyndicationItem(this.BaseUri, resource)); } }
/// <summary> /// Gets a media contents of a member resource. /// </summary> /// <param name="collectionName">Name of the target collection.</param> /// <param name="memberResourceId">The Id of the member resource.</param> /// <param name="outputStream">A stream containing the media corresponding to the specified resource. If no matching media is found, null is returned.</param> /// <exception cref="ArgumentNullException">Throws exception if memberResourceId is null/empty /// or outputStream is null.</exception> /// <exception cref="ArgumentException">Throws exception if outputStream stream is not readable.</exception> void IAtomPubStoreReader.GetMedia(string collectionName, string memberResourceId, Stream outputStream) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (string.IsNullOrEmpty(memberResourceId)) { throw new ArgumentNullException("memberResourceId"); } if (null == outputStream) { throw new ArgumentNullException("outputStream"); } if (!AtomPubHelper.IsValidGuid(memberResourceId)) { throw new ArgumentException(Resources.ATOMPUB_INVALID_RESOURCE_ID); } if (!outputStream.CanWrite) { throw new ArgumentException(Properties.Resources.ATOMPUB_CANNOT_WRITE_ON_STREAM, "outputStream"); } using (ZentityContext context = CoreHelper.CreateZentityContext()) { ResourceType collectionType = coreHelper.GetResourceType(collectionName); // Prepare a query to get a resource with specified Id and specified type. string commandText = string.Format(CultureInfo.InvariantCulture, AtomPubConstants.EsqlToGetFileContents, collectionType.FullName); ObjectQuery <Core.File> query = new ObjectQuery <Core.File>(commandText, context); query.Parameters.Add(new ObjectParameter("Id", new Guid(memberResourceId))); Core.File mediaFile = query.FirstOrDefault(); if (null == mediaFile) { throw new ResourceNotFoundException(Resources.ATOMPUB_RESOURCE_NOT_FOUND); } if (!mediaFile.Authorize("Read", context, CoreHelper.GetAuthenticationToken())) { throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED); } context.DownloadFileContent(mediaFile, outputStream); } }
/// <summary> /// Deletes the Resource.File for the specified resource. /// </summary> /// <param name="collectionName">The type of the resource.</param> /// <param name="memberResourceId">The Guid of the resource.</param> /// <returns>True if the operation succeeds, False otherwise.</returns> /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty.</exception> /// <exception cref="ArgumentException">Throws exception if requested memberResourceId is not a unique identifier.</exception> bool IAtomPubStoreWriter.DeleteMedia(string collectionName, string memberResourceId) { if (string.IsNullOrEmpty(collectionName)) { throw new ArgumentNullException("collectionName"); } if (!AtomPubHelper.IsValidGuid(memberResourceId)) { throw new ArgumentException(Resources.ATOMPUB_INVALID_RESOURCE_ID, "memberResourceId"); } using (ZentityContext context = CoreHelper.CreateZentityContext()) { Type collectionType = CoreHelper.GetSystemResourceType(collectionName); string commandText = string.Format(CultureInfo.InvariantCulture, AtomPubConstants.EsqlToGetFileContents, collectionType.FullName); ObjectQuery <Core.File> query = new ObjectQuery <Core.File>(commandText, context); query.Parameters.Add(new ObjectParameter("Id", new Guid(memberResourceId))); Core.File mediaFile = query.FirstOrDefault(); if (null == mediaFile) { throw new ResourceNotFoundException(Resources.ATOMPUB_RESOURCE_NOT_FOUND); } if (!mediaFile.Authorize("Delete", context, CoreHelper.GetAuthenticationToken())) { throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED); } DeleteRelationships(context, mediaFile); context.DeleteObject(mediaFile); context.SaveChanges(); return(true); } }