Ejemplo n.º 1
0
        public bool Save(MediaResourceCreateCommand command, User createdByUser, out string failureReason, out MediaResource mediaResource)
        {
            failureReason = string.Empty;
            mediaResource = null;

            if (!_documentSession.Load <AppRoot>(Constants.AppRootId).ImageServiceStatus)
            {
                failureReason = "Image files cannot be uploaded at the moment. Please try again later.";
                return(false);
            }

            ImageUtility image = null;
            bool         returnValue;

            try
            {
                var imageCreationTasks = new List <ImageCreationTask>();

                image = ImageUtility.Load(command.FileStream);

                if (command.Usage == "contribution")
                {
                    mediaResource = MakeContributionImage(
                        createdByUser,
                        image,
                        imageCreationTasks,
                        command);
                }
                else if (command.Usage == "avatar")
                {
                    mediaResource = MakeAvatarImage(
                        createdByUser,
                        image,
                        imageCreationTasks,
                        command);
                }
                else if (command.Usage == "background")
                {
                    mediaResource = MakeBackgroundImage(
                        createdByUser,
                        image,
                        imageCreationTasks,
                        command);
                }
                else
                {
                    throw new ArgumentException("The specified usage '" + command.Usage + "' is not recognised.");
                }

                image.Save(mediaResource, imageCreationTasks, _mediaFilePathFactory);

                returnValue = true;
            }
            catch (Exception exception)
            {
                _logger.ErrorException("Error saving images", exception);

                failureReason = "The file is corrupted or not a valid JPEG and could not be saved. Please check the file and try again.";
                returnValue   = false;
            }
            finally
            {
                if (image != null)
                {
                    image.Cleanup();
                }
            }

            return(returnValue);
        }