Exemple #1
0
        /// <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>
 /// Adds the file resource.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="resource">The resource.</param>
 /// <param name="mediaFilePath">The media file path.</param>
 /// <returns>The added <see cref="Core.File"/> type.</returns>
 private static Core.File AddFileResource(
     ZentityContext context,
     ScholarlyWork resource,
     string mediaFilePath)
 {
     Core.File mediaResource = AddFileResource(context, resource);
     context.UploadFileContent(mediaResource, mediaFilePath);
     return(mediaResource);
 }
 /// <summary>
 /// Adds the file resource.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="resource">The resource.</param>
 /// <param name="mediaStream">The media stream.</param>
 /// <returns>The added <see cref="Core.File"/> type.</returns>
 private static Core.File AddFileResource(
     ZentityContext context,
     ScholarlyWork resource,
     Stream mediaStream)
 {
     Core.File mediaResource = AddFileResource(context, resource);
     mediaStream.Position = 0;
     context.UploadFileContent(mediaResource, mediaStream);
     return(mediaResource);
 }
Exemple #4
0
        /// <summary>
        /// Creates a new Resource.File for a specified resource of type collectionName in the repository.
        /// </summary>
        /// <param name="collectionName">The resource type.</param>
        /// <param name="mimeType">The MIME type of media.</param>
        /// <param name="media">The new File contents.</param>
        /// <param name="fileExtension">The media file extension.</param>
        /// <returns>A SyndicationItem that describes the newly created resource.</returns>
        /// <exception cref="ArgumentNullException">Throws exception if collectionName is null/empty
        /// or mimeType is null/empty or media is null.</exception>
        protected SyndicationItem CreateMedia(string collectionName, string mimeType, byte[] media,
                                              string fileExtension)
        {
            if (string.IsNullOrEmpty(collectionName))
            {
                throw new ArgumentNullException("collectionName");
            }

            if (string.IsNullOrEmpty(mimeType))
            {
                throw new ArgumentNullException("mimeType");
            }

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

            AuthenticatedToken authenticatedToken = CoreHelper.GetAuthenticationToken();

            using (ZentityContext context = CoreHelper.CreateZentityContext())
            {
                if (!authenticatedToken.HasCreatePermission(context))
                {
                    throw new UnauthorizedException(Resources.ATOMPUB_UNAUTHORIZED);
                }

                ScholarlyWork resource = CreateScholarlyWork(collectionName);
                resource.DateModified = DateTime.Now;

                Core.File mediaResource = new Core.File();
                mediaResource.MimeType      = mimeType;
                mediaResource.FileExtension = string.IsNullOrEmpty(fileExtension) ?
                                              AtomPubHelper.GetFileExtension(mimeType) : fileExtension;
                context.AddToResources(mediaResource);
                context.SaveChanges();
                resource.Files.Add(mediaResource);

                MemoryStream mediaStream = ZentityAtomPubStoreWriter.GetMediaStream(media);
                context.UploadFileContent(mediaResource, mediaStream);
                mediaStream.Close();

                resource.GrantDefaultPermissions(context, authenticatedToken);
                mediaResource.GrantDefaultPermissions(context, authenticatedToken);

                context.SaveChanges();

                return(ZentityAtomPubStoreReader.GenerateSyndicationItem(this.BaseUri, resource));
            }
        }