Пример #1
0
        /// <summary>
        /// Attach a file
        /// </summary>
        /// <param name="file">File to attach</param>
        /// <param name="filePredicate">Predicate of the ontological property</param>
        /// <param name="fileName">File name</param>
        /// <param name="fileType">File type</param>
        /// <param name="onlyReference">True if the file mustn't be saved, only as a reference</param>
        /// <param name="entity">(Optional) The auxiliary entity that owns the file property</param>
        /// <param name="language">language of the file</param>
        private void AttachFileInternal(byte[] file, string filePredicate, string fileName, AttachedResourceFilePropertyTypes fileType, OntologyEntity entity = null, bool onlyReference = false, string language = null)
        {
            if (file != null)
            {
                if (onlyReference == false)
                {
                    AttachedFiles.Add(file);

                    string temporalFileName = fileName;
                    if (!string.IsNullOrEmpty(language))
                    {
                        temporalFileName += $"@{language}";
                    }

                    AttachedFilesName.Add(temporalFileName);

                    if (fileType == AttachedResourceFilePropertyTypes.file)
                    {
                        AttachedFilesType.Add(AttachedResourceFilePropertyTypes.file);
                    }
                    else
                    {
                        AttachedFilesType.Add(AttachedResourceFilePropertyTypes.downloadableFile);
                    }
                }
            }

            if (entity != null)
            {
                if (Ontology.Entities == null)
                {
                    Ontology.Entities = new List <OntologyEntity>();
                    Ontology.Entities.Add(entity);
                }
                if (AttachedFilesType.Count > 0 && AttachedFiles.Count > 0 && AttachedFilesType.Count > 0)
                {
                    entity.Properties.Add(new StringOntologyProperty(filePredicate, fileName, language));
                }
            }
            else
            {
                if (filePredicate != null)
                {
                    if (Ontology.Properties == null)
                    {
                        Ontology.Properties = new List <OntologyProperty>();
                    }
                    Ontology.Properties.Add(new StringOntologyProperty(filePredicate, fileName, language));
                }
            }

            _rdfFile = Ontology.GenerateRDF();
        }
Пример #2
0
        /// <summary>
        /// Attach an image
        /// </summary>
        /// <param name="originalImage">Image to upload,  null if it would be only a reference</param>
        /// <param name="actions">List of <see cref="ImageAction"/></param>
        /// <param name="predicate">Ontology predicate</param>
        /// <param name="mainImage">True if the image is the resource main image</param>
        /// <param name="imageId">Image identifier. If it's Guid.Empty, it would be automatically generated</param>
        /// <param name="onlyReference">True if the image mustn't be saved, only as a reference</param>
        /// <param name="entity">(Optional) The auxiliary entity that owns the image property</param>
        /// <param name="extension">Image extension</param>
        /// <param name="saveOriginalImage">True if the original file must be saved</param>
        /// <param name="saveMaxSizedImage">True if the max sized image must be saved</param>
        /// <param name="saveMainImage">True if the main image must be saved</param>
        /// <returns>True if the image reference has been attached successfully</returns>
        private bool AttachImageInternal(byte[] originalImage, List <ImageAction> actions, string predicate, bool mainImage, Guid imageId, bool onlyReference, OntologyEntity entity, string extension, bool saveOriginalImage = true, bool saveMaxSizedImage = false, bool saveMainImage = true)
        {
            if (string.IsNullOrEmpty(extension))
            {
                extension = ".jpg";
            }
            else
            {
                if (!extension.StartsWith("."))
                {
                    extension = "." + extension;
                }
            }

            if (imageId.Equals(Guid.Empty))
            {
                imageId = Guid.NewGuid();
            }

            bool attachedImage = false;

            try
            {
                List <string> errorList = new List <string>();

                if (actions == null)
                {
                    // Without actions
                    if (mainImage && saveOriginalImage)
                    {
                        // The image to upload has lower size than 240 pixels. The image quality would be 100%
                        MainImage = string.Format("[IMGPrincipal][240,]{0}" + extension, imageId);

                        AttachedFilesName.Add($"{imageId}" + extension);
                        AttachedFilesType.Add(AttachedResourceFilePropertyTypes.image);

                        AttachedFilesName.Add($"{imageId}_240" + extension);
                        AttachedFilesType.Add(AttachedResourceFilePropertyTypes.image);

                        if (!onlyReference)
                        {
                            AttachedFiles.Add(originalImage);
                            AttachedFiles.Add(originalImage);
                            AttachedFiles.Add(originalImage);
                        }
                        attachedImage = true;
                    }
                    else if (saveOriginalImage)
                    {
                        AttachedFilesName.Add($"{imageId}" + extension);
                        AttachedFilesType.Add(AttachedResourceFilePropertyTypes.image);

                        if (!onlyReference)
                        {
                            AttachedFiles.Add(originalImage);
                        }
                        attachedImage = true;
                    }
                    else
                    {
                        throw new GnossAPIException("Actions can be null only when the image is a main image and the original image is set to be saved");
                    }
                }
                else
                {
                    // With actions

                    bool   originalImageSaved     = false;
                    Bitmap maxSizeImage           = null;
                    bool   imageModificationError = false;

                    foreach (ImageAction action in actions)
                    {
                        imageModificationError = false;
                        Bitmap resizedImage = null;

                        if (!onlyReference)
                        {
                            // Modify the image
                            switch (action.ImageTransformationType)
                            {
                            case ImageTransformationType.Crop:
                                try
                                {
                                    resizedImage = ImageHelper.CropImageToSquare(ImageHelper.ByteArrayToBitmap(originalImage), action.Size);
                                }
                                catch (GnossAPIException gaex)
                                {
                                    imageModificationError = true;
                                    errorList.Add(gaex.Message);
                                }
                                break;

                            case ImageTransformationType.ResizeToHeight:
                                try
                                {
                                    resizedImage = ImageHelper.ResizeImageToHeight(ImageHelper.ByteArrayToBitmap(originalImage), action.Height);
                                }
                                catch (GnossAPIException gaex)
                                {
                                    imageModificationError = true;
                                    errorList.Add(gaex.Message);
                                }
                                break;

                            case ImageTransformationType.ResizeToWidth:
                                try
                                {
                                    resizedImage = ImageHelper.ResizeImageToWidth(ImageHelper.ByteArrayToBitmap(originalImage), action.Width);
                                }
                                catch (GnossAPIException gaex)
                                {
                                    imageModificationError = true;
                                    errorList.Add(gaex.Message);
                                }
                                break;

                            case ImageTransformationType.ResizeToHeightAndWidth:
                                try
                                {
                                    resizedImage = ImageHelper.ResizeImageToHeightAndWidth(ImageHelper.ByteArrayToBitmap(originalImage), action.Width, action.Height);
                                }
                                catch (GnossAPIException gaex)
                                {
                                    imageModificationError = true;
                                    errorList.Add(gaex.Message);
                                }
                                break;

                            case ImageTransformationType.CropToHeightAndWidth:
                                try
                                {
                                    resizedImage = ImageHelper.CropImageToHeightAndWidth(ImageHelper.ByteArrayToBitmap(originalImage), action.Height, action.Width);
                                }
                                catch (GnossAPIException gaex)
                                {
                                    imageModificationError = true;
                                    errorList.Add(gaex.Message);
                                }
                                break;

                            default:
                                throw new GnossAPIException("The ImageTransformationType is not valid");
                            }
                        }

                        if (mainImage && imageModificationError)
                        {
                            mainImage = false;
                        }

                        if (!imageModificationError && !onlyReference)
                        {
                            if (mainImage)
                            {
                                MainImage = string.Format("[IMGPrincipal][{0},]{1}" + extension, action.Size, imageId);
                                mainImage = false;
                            }

                            AttachedFilesName.Add($"{imageId}_{action.Size}" + extension);
                            AttachedFiles.Add(ImageHelper.BitmapToByteArray(resizedImage, action.ImageQualityPercentage));
                            AttachedFilesType.Add(AttachedResourceFilePropertyTypes.image);
                            attachedImage = true;

                            if (action.EmbedsRGB)
                            {
                                resizedImage = ImageHelper.AssignEXIFPropertyColorSpaceSRGB(resizedImage);
                            }

                            // Save original image
                            if (saveOriginalImage && !originalImageSaved)
                            {
                                if (saveMainImage)
                                {
                                    AttachedFilesName.Add($"{imageId}" + extension);
                                    AttachedFiles.Add(ImageHelper.BitmapToByteArray(ImageHelper.ByteArrayToBitmap(originalImage), actions.Max(z => z.ImageQualityPercentage)));
                                    AttachedFilesType.Add(AttachedResourceFilePropertyTypes.image);
                                }

                                originalImageSaved = true;
                                attachedImage      = true;
                            }
                        }
                        else
                        {
                            if (mainImage)
                            {
                                MainImage = string.Format("[IMGPrincipal][{0},]{1}" + extension, action.Size, imageId);
                                mainImage = false;
                            }
                            attachedImage = true;
                        }
                    }

                    // Save the image at the max size allowed
                    if (saveMaxSizedImage)
                    {
                        try
                        {
                            // The quality percentage of the max size image is the highest quality percentage
                            if (ImageHelper.ByteArrayToBitmap(originalImage).Width > Constants.MAXIMUM_WIDTH_GNOSS_IMAGE)
                            {
                                maxSizeImage = ImageHelper.ResizeImageToWidth(ImageHelper.ByteArrayToBitmap(originalImage), Constants.MAXIMUM_WIDTH_GNOSS_IMAGE);

                                AttachedFilesName.Add($"{imageId}_{Constants.MAXIMUM_WIDTH_GNOSS_IMAGE}" + extension);
                                AttachedFiles.Add(ImageHelper.BitmapToByteArray(maxSizeImage, actions.Max(z => z.ImageQualityPercentage)));
                                AttachedFilesType.Add(AttachedResourceFilePropertyTypes.image);

                                attachedImage = true;
                            }
                            else
                            {
                                AttachedFilesName.Add($"{imageId}_{Constants.MAXIMUM_WIDTH_GNOSS_IMAGE}" + extension);
                                AttachedFiles.Add(ImageHelper.BitmapToByteArray(ImageHelper.ByteArrayToBitmap(originalImage), actions.Max(z => z.ImageQualityPercentage)));
                                AttachedFilesType.Add(AttachedResourceFilePropertyTypes.image);

                                attachedImage = true;
                            }
                        }
                        catch (GnossAPIException gaex)
                        {
                            imageModificationError = true;
                            errorList.Add(gaex.Message);
                        }
                    }
                }

                if (entity != null && !string.IsNullOrWhiteSpace(predicate) && !string.IsNullOrEmpty(predicate))
                {
                    // The image is from an auxiliary entity
                    if (Ontology.Entities == null)
                    {
                        Ontology.Entities = new List <OntologyEntity>();
                        Ontology.Entities.Add(entity);
                    }

                    if (!onlyReference)
                    {
                        if (AttachedFiles.Count > 0 && AttachedFilesType.Count > 0)
                        {
                            if (entity.Properties == null)
                            {
                                entity.Properties = new List <OntologyProperty>();
                            }
                            entity.Properties.Add(new ImageOntologyProperty(predicate, $"{imageId}" + extension));
                        }
                    }
                    else
                    {
                        if (attachedImage)
                        {
                            if (entity.Properties == null)
                            {
                                entity.Properties = new List <OntologyProperty>();
                            }
                            entity.Properties.Add(new ImageOntologyProperty(predicate, $"{imageId}" + extension));
                        }
                    }
                }
                else
                {
                    // The image is an ontology property
                    if (!string.IsNullOrWhiteSpace(predicate) && !string.IsNullOrEmpty(predicate))
                    {
                        if (Ontology.Properties == null)
                        {
                            Ontology.Properties = new List <OntologyProperty>();
                        }

                        if (!onlyReference)
                        {
                            if (AttachedFilesType.Count > 0 && AttachedFiles.Count > 0)
                            {
                                ImageOntologyProperty pOntImg = new ImageOntologyProperty(predicate, $"{imageId}" + extension);
                                Ontology.Properties.Add(pOntImg);
                            }
                        }
                        else
                        {
                            if (attachedImage)
                            {
                                Ontology.Properties.Add(new ImageOntologyProperty(predicate, $"{imageId}" + extension));
                            }
                        }
                    }
                }

                if (errorList != null && errorList.Count > 0)
                {
                    string message = null;
                    foreach (string error in errorList)
                    {
                        if (message == null)
                        {
                            message = error;
                        }
                        else
                        {
                            message = $"{message}\n{error}";
                        }
                    }
                    throw new GnossAPIException(message);
                }
            }
            catch (GnossAPIException)
            {
                throw;
            }
            catch (FileNotFoundException ex)
            {
                throw new GnossAPIException($"The image: {imageId} doesn't exist or is inaccessible", ex);
            }

            _rdfFile = Ontology.GenerateRDF();
            return(attachedImage);
        }