示例#1
0
 public void UpdateFiles(List <AttachedFilesVM> files)
 {
     if (files == null || files.Count() == 0)
     {
         return;
     }
     foreach (var item in files)
     {
         if (this.AttachedFiles.Any(q => q.Id == item.Id))
         {
             var obj = this.AttachedFiles.FirstOrDefault(q => q.Id == item.Id);
             obj.Order = item.Order;
             obj.UpdateTranslations(item.Translations);
         }
         else
         {
             if (item.Translations.Any(x => !string.IsNullOrWhiteSpace(x.File)))
             {
                 var newFile = new AttachedFiles()
                 {
                     Order = item.Order
                 };
                 newFile.UpdateTranslations(item.Translations);
                 this.AttachedFiles.Add(newFile);
             }
         }
     }
 }
示例#2
0
        private void OnAttach()
        {
            var fileInfo = Service.GetFile("Выберите файл для прикрепления", "Все файлы (*.*)|*.*").FullName;

            AttachedFiles.Add(new AttachedFiles {
                fileName = Path.GetFileName(fileInfo), fullName = fileInfo
            });
        }
示例#3
0
        /// <summary>
        /// 取得檔案資訊
        /// </summary>
        /// <returns></returns>
        public AttachedFiles GetFileInfo()
        {
            AttachedFiles attachedFiles = new AttachedFiles
            {
                File_Path = FilePath
            };

            return(attachedFiles);
        }
示例#4
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();
        }
        private void AttachFile(object o)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Multiselect = true;
            if (ofd.ShowDialog() == true)
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    foreach (var i in ofd.FileNames)
                    {
                        AttachedFiles.Add(i);
                    }
                });
            }
        }
        void Attachments_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                ClientStats.LogEvent("Delete attachment from new message");

                var attachment = (AttachmentDataHelper)AttachmentsListView.SelectedItem;

                AttachedFiles.Remove(attachment);

                if (IsDraft && attachment.DocumentId.HasValue)
                {
                    // Also remove version that is attached to our draft message
                    var document = SourceMessage.Documents.FirstOrDefault(d => d.DocumentId == attachment.DocumentId);

                    if (document != null)
                    {
                        document.DeleteFrom(SourceMessage);
                    }
                }

                e.Handled = true;
            }
        }
示例#7
0
        List <LocalFile> BuildLocalFiles()
        {
            Image            image = null;
            List <LocalFile> files = new List <LocalFile>();

            if (AttachedFiles != null && AttachedFiles.Count > 0)
            {
                foreach (var item in AttachedFiles)
                {
                    var file = new LocalFile
                    {
                        Path = item,
                        Name = Path.GetFileName(item),
                        Size = (ulong)new System.IO.FileInfo(item).Length,
                        Mime = Mime.GetMimeType(Path.GetExtension(item))
                    };
                    file.UploadProgressHandler = (r) =>
                    {
                        if (r.IsSuccess)
                        {
                            Output($"{file.Name} is uploading {r.Data}%");
                        }
                    };

                    if (IsImage(item))
                    {
                        // Get thumbnail
                        image = Image.FromFile(item);
                        var thumbnail = image?.GetThumbnailImage(image.Width, image.Height, null, System.IntPtr.Zero);
                        if (thumbnail != null)
                        {
                            // Save to file
                            var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + System.Diagnostics.Process.GetCurrentProcess().ProcessName + "\\thumbnails\\";
                            try
                            {
                                System.IO.Directory.CreateDirectory(path);
                            }
                            catch (Exception e)
                            {
                                Output($"{e.Message}");
                            }

                            string name     = "thumb-" + Guid.NewGuid().ToString() + "-" + Path.GetFileName(file.Path);
                            var    fullPath = path + name;
                            thumbnail.Save(fullPath);

                            var mimeType = Mime.GetMimeType(Path.GetExtension(fullPath));

                            // Build Thumbnail
                            file.LocalThumbnail = new LocalFile.Thumbnail()
                            {
                                Width  = thumbnail.Width,
                                Height = thumbnail.Height,
                                Mime   = mimeType,
                                Path   = fullPath,
                            };
                        }
                    }

                    files.Add(file);
                }
                AttachedFiles.Clear();
            }
            return(files);
        }
示例#8
0
 private void OnClear()
 {
     AttachedFiles.Clear();
 }
示例#9
0
 private void OnDelete()
 {
     AttachedFiles.Remove(AttachedFile);
 }
示例#10
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);
        }