コード例 #1
0
        /// <summary>
        /// Attach a reference to a file (not an image, to attach an image use AttachImageWithoutReference method) previusly uploaded using the <see cref="AttachFileWithoutReference"/> method
        /// </summary>
        /// <param name="downloadUrl">Download Url. It can be a local path or a internet url</param>
        /// <param name="filePredicate">Predicate of the ontological property where the file reference will be inserted</param>
        /// <param name="entity">(Optional) Auxiliary entity which would have the reference to the file</param>
        /// <param name="fileIdentifier">(Optional) Unique identifier of the file. Only neccesary if there is more than one file with the same name</param>
        /// <param name="language">(Optional) The file language</param>
        public void AttachReferenceToFile(string downloadUrl, string filePredicate, OntologyEntity entity = null, string fileIdentifier = null, string language = null)
        {
            if (!string.IsNullOrEmpty(downloadUrl))
            {
                string fileName = string.Empty;
                if (Uri.IsWellFormedUriString(downloadUrl, UriKind.Absolute))
                {
                    fileName = ((!string.IsNullOrEmpty(fileIdentifier)) ? $"{fileIdentifier}_" : "") + downloadUrl.Substring(downloadUrl.LastIndexOf("/") + 1);
                }
                else
                {
                    if (File.Exists(downloadUrl))
                    {
                        if (downloadUrl.Contains("/"))
                        {
                            fileName = downloadUrl.Substring(downloadUrl.LastIndexOf("/") + 1);
                        }
                        else if (downloadUrl.Contains("\\"))
                        {
                            fileName = downloadUrl.Substring(downloadUrl.LastIndexOf("\\") + 1);
                        }
                    }
                    else
                    {
                        throw new GnossAPIException($"The file: {downloadUrl} doesn't exist or is inaccessible");
                    }
                }

                if (language != null)
                {
                    fileName = $"{fileName}@{language}";
                }

                AttachFileInternal(null, filePredicate, fileName, AttachedResourceFilePropertyTypes.file, entity, true);
            }
            else
            {
                throw new GnossAPIArgumentException("Required. It can't be null or empty", "downloadUrl");
            }
        }
コード例 #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);
        }
コード例 #3
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();
        }
コード例 #4
0
        /// <summary>
        /// Attach an image to the resource. It generates as many images as actions contains the actions parameter
        /// </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="extension">Image extension</param>
        /// <param name="entity">(Optional) The auxiliary entity that owns the image property</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>
        /// <remarks>If a main image has lower size than the minimum permited (240px), acions must be null</remarks>
        /// <returns>True if the image reference has been attached successfully</returns>
        public bool AttachImage(byte[] originalImage, List <ImageAction> actions, string predicate, bool mainImage, string extension, OntologyEntity entity = null, bool saveOriginalImage = true, bool saveMaxSizedImage = false)
        {
            bool success = false;

            if (originalImage != null)
            {
                success = AttachImageInternal(originalImage, actions, predicate, mainImage, Guid.Empty, false, entity, extension, saveOriginalImage, saveMaxSizedImage);
            }
            return(success);
        }
コード例 #5
0
 /// <summary>
 /// Attach an image to the resource previusly uploaded using the AttachImageWithoutReference method
 /// </summary>
 /// <param name="actions">List of transformations to do over the image</param>
 /// <param name="predicate">Predicate of the ontological property where the image reference will be inserted</param>
 /// <param name="mainImage">True if this image is the resource main image</param>
 /// <param name="entity">(Optional) Auxiliary entity which would have the reference to the image</param>
 /// <param name="imageId">Image identifier. If it's Guid.Empty, it would be automatically generated</param>
 /// <param name="extension">Image extension</param>
 /// <remarks>If a main image has lower size than the minimum permited (240px), acions must be null</remarks>
 /// <returns>True if the image reference has been attached successfully</returns>
 public bool AttachReferenceToImage(List <ImageAction> actions, string predicate, bool mainImage, Guid imageId, string extension, OntologyEntity entity = null)
 {
     return(AttachImageInternal(null, actions, predicate, mainImage, imageId, true, entity, extension, false, false, false));
 }
コード例 #6
0
        /// <summary>
        /// Attach an image to the resource. It generates as many images as actions contains the actions parameter
        /// </summary>
        /// <param name="downloadUrl">Download url of the image. It can be a local path or a internet url</param>
        /// <param name="actions">List of transformations to do over the image</param>
        /// <param name="predicate">Predicate of the ontological property where the image reference will be inserted</param>
        /// <param name="mainImage">True if this image is the resource main image</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="entity">(Optional) Auxiliary entity which would have the reference to the image</param>
        /// <remarks>If a main image has lower size than the minimum permited (240px), acions must be null</remarks>
        /// <returns>True if the image has been attached successfully</returns>
        public bool AttachImage(string downloadUrl, List <ImageAction> actions, string predicate, bool mainImage, string extension, OntologyEntity entity = null, bool saveOriginalImage = true, bool saveMaxSizedImage = false)
        {
            Bitmap imagenOriginal  = ImageHelper.ReadImageFromUrlOrLocalPath(downloadUrl);
            bool   imagenAdjuntada = false;

            if (imagenOriginal != null)
            {
                imagenAdjuntada = AttachImageInternal(ImageHelper.BitmapToByteArray(imagenOriginal), actions, predicate, mainImage, Guid.Empty, false, entity, extension, saveOriginalImage, saveMaxSizedImage);
            }
            return(imagenAdjuntada);
        }
コード例 #7
0
 /// <summary>
 /// Attach a file (not an image, to attach an image use AttachImage method) to the resource that can be accessed from the Web. It means that the file won't be encripted in the server
 /// </summary>
 /// <param name="file">Archivo a adjuntar (no imagen).</param>
 /// <param name="filePredicate">Predicate of the ontological property where the file reference will be inserted</param>
 /// <param name="entity">(Optional) Auxiliary entity which would have the reference to the file</param>
 /// <param name="fileName">The file name</param>
 /// <param name="language">(Optional) The file language</param>
 public void AttachDownloadableFile(byte[] file, string filePredicate, string fileName, OntologyEntity entity = null, string language = null)
 {
     AttachFileInternal(file, filePredicate, fileName, AttachedResourceFilePropertyTypes.downloadableFile, entity, false, language);
 }
コード例 #8
0
 /// <summary>
 /// Attach a file (not an image, to attach an image use AttachImage method) to the resource.
 /// </summary>
 /// <param name="file">The file bytes</param>
 /// <param name="filePredicate">Predicate of the ontological property where the file reference will be inserted</param>
 /// <param name="fileName">The file name</param>
 /// <param name="entity">(Optional) Auxiliary entity which would have the reference to the file</param>
 public void AttachFile(byte[] file, string filePredicate, string fileName, OntologyEntity entity = null)
 {
     AttachFileInternal(file, filePredicate, fileName, AttachedResourceFilePropertyTypes.file, entity);
 }