Exemplo n.º 1
0
 public byte[] ExportMetadata(PostedDataDto postedData)
 {
     using (MetadataContext context = new MetadataContext(metadataConfiguration.GetAbsolutePath(postedData.guid), postedData.password))
     {
         return(context.ExportProperties());
     }
 }
Exemplo n.º 2
0
        public DocumentPreviewDto LoadDocument(PostedDataDto postedData)
        {
            string password        = string.IsNullOrEmpty(postedData.password) ? null : postedData.password;
            var    documentPreview = new DocumentPreviewDto();

            // set password for protected document
            var loadOptions = new LoadOptions
            {
                Password = password
            };


            bool completed = ExecuteWithTimeLimit(TimeSpan.FromMilliseconds(metadataConfiguration.GetPreviewTimeLimit()), cancelationToken =>
            {
                using (Stream fileStream = fileService.GetSourceFileStream(postedData.guid))
                    using (GroupDocs.Metadata.Metadata metadata = new GroupDocs.Metadata.Metadata(fileStream, loadOptions))
                    {
                        cancelationToken.ThrowIfCancellationRequested();
                        IReadOnlyList <PageInfo> pages = metadata.GetDocumentInfo().Pages;

                        using (MemoryStream stream = new MemoryStream())
                        {
                            PreviewOptions previewOptions = new PreviewOptions(pageNumber => stream, (pageNumber, pageStream) => { });
                            previewOptions.PreviewFormat  = PreviewOptions.PreviewFormats.PNG;

                            int pageCount = pages.Count;
                            if (metadataConfiguration.GetPreloadPageCount() > 0)
                            {
                                pageCount = metadataConfiguration.GetPreloadPageCount();
                            }

                            for (int i = 0; i < pageCount; i++)
                            {
                                cancelationToken.ThrowIfCancellationRequested();
                                previewOptions.PageNumbers = new[] { i + 1 };
                                try
                                {
                                    metadata.GeneratePreview(previewOptions);
                                }
                                catch (NotSupportedException)
                                {
                                    break;
                                }

                                PageDescriptionEntity pageData = GetPageDescriptionEntities(pages[i]);
                                string encodedImage            = Convert.ToBase64String(stream.ToArray());
                                pageData.SetData(encodedImage);
                                documentPreview.SetPages(pageData);
                                stream.SetLength(0);
                            }
                        }
                    }
            });

            documentPreview.SetTimeLimitExceeded(!completed);
            documentPreview.SetGuid(postedData.guid);

            // return document description
            return(documentPreview);
        }
Exemplo n.º 3
0
 public byte[] ExportMetadata(PostedDataDto postedData)
 {
     using (var inputStream = fileService.GetFileStream(postedData.guid))
         using (MetadataContext context = new MetadataContext(inputStream, postedData.password))
         {
             return(context.ExportProperties());
         }
 }
Exemplo n.º 4
0
 private void UpdateMetadata(PostedDataDto postedData, Action <MetadataContext> updateMethod)
 {
     using (var inputStream = fileService.GetFileStream(postedData.guid, false))
         using (MetadataContext context = new MetadataContext(inputStream, postedData.password))
         {
             updateMethod(context);
             context.Save();
         }
 }
Exemplo n.º 5
0
 public void SaveProperties(PostedDataDto postedData)
 {
     UpdateMetadata(postedData, (context) =>
     {
         foreach (var packageInfo in postedData.packages)
         {
             context.UpdateProperties(packageInfo.id, packageInfo.properties.Select(p => new Property(p.name, (PropertyType)p.type, p.value)));
         }
     });
 }
Exemplo n.º 6
0
 public void RemoveProperties(PostedDataDto postedData)
 {
     UpdateMetadata(postedData, (context) =>
     {
         foreach (var packageInfo in postedData.packages)
         {
             context.RemoveProperties(packageInfo.id, packageInfo.properties.Select(p => p.name));
         }
     });
 }
 public HttpResponseMessage LoadProperties(PostedDataDto postedData)
 {
     try
     {
         return(Request.CreateResponse(HttpStatusCode.OK, metadataService.GetPackages(postedData)));
     }
     catch (Exception ex)
     {
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, Resources.GenerateException(ex)));
     }
 }
 public HttpResponseMessage SaveProperties(PostedDataDto postedData)
 {
     try
     {
         metadataService.SaveProperties(postedData);
         return(Request.CreateResponse(HttpStatusCode.OK, new object()));
     }
     catch (Exception ex)
     {
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, Resources.GenerateException(ex)));
     }
 }
        public LoadDocumentEntity LoadDocument(PostedDataDto postedData)
        {
            // get/set parameters
            string             filePath           = metadataConfiguration.GetAbsolutePath(postedData.guid);
            string             password           = string.IsNullOrEmpty(postedData.password) ? null : postedData.password;
            LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity();

            // set password for protected document
            var loadOptions = new LoadOptions
            {
                Password = password
            };

            using (GroupDocs.Metadata.Metadata metadata = new GroupDocs.Metadata.Metadata(filePath, loadOptions))
            {
                GroupDocs.Metadata.Common.IReadOnlyList <PageInfo> pages = metadata.GetDocumentInfo().Pages;

                using (MemoryStream stream = new MemoryStream())
                {
                    PreviewOptions previewOptions = new PreviewOptions(pageNumber => stream, (pageNumber, pageStream) => { });
                    previewOptions.PreviewFormat = PreviewOptions.PreviewFormats.PNG;

                    int pageCount = pages.Count;
                    if (metadataConfiguration.GetPreloadPageCount() > 0)
                    {
                        pageCount = metadataConfiguration.GetPreloadPageCount();
                    }
                    for (int i = 0; i < pageCount; i++)
                    {
                        previewOptions.PageNumbers = new[] { i + 1 };
                        try
                        {
                            metadata.GeneratePreview(previewOptions);
                        }
                        catch (NotSupportedException)
                        {
                            continue;
                        }

                        PageDescriptionEntity pageData = GetPageDescriptionEntities(pages[i]);
                        string encodedImage            = Convert.ToBase64String(stream.ToArray());
                        pageData.SetData(encodedImage);
                        loadDocumentEntity.SetPages(pageData);
                        stream.SetLength(0);
                    }
                }
            }

            loadDocumentEntity.SetGuid(postedData.guid);

            // return document description
            return(loadDocumentEntity);
        }
Exemplo n.º 10
0
        public void CleanMetadata(PostedDataDto postedData)
        {
            string filePath     = metadataConfiguration.GetAbsolutePath(postedData.guid);
            var    tempFilePath = GetTempPath(filePath);

            using (MetadataContext context = new MetadataContext(filePath, postedData.password))
            {
                context.Sanitize();
                context.Save(tempFilePath);
            }
            DirectoryUtils.MoveFile(tempFilePath, filePath);
        }
Exemplo n.º 11
0
        public IEnumerable <ExtractedPackageDto> GetPackages(PostedDataDto postedData)
        {
            using (var inputStream = fileService.GetFileStream(postedData.guid))
                using (MetadataContext context = new MetadataContext(inputStream, postedData.password))
                {
                    var packages = new List <ExtractedPackageDto>();
                    foreach (var package in context.GetPackages())
                    {
                        List <PropertyDto>      properties  = new List <PropertyDto>();
                        List <KnownPropertyDto> descriptors = new List <KnownPropertyDto>();

                        foreach (var property in package.Properties)
                        {
                            properties.Add(new PropertyDto
                            {
                                name  = property.Name,
                                value = property.Value is Array ? ArrayUtil.AsString((Array)property.Value) : property.Value,
                                type  = (int)property.Type,
                            });
                        }

                        foreach (var descriptor in package.Descriptors)
                        {
                            var accessLevel = descriptor.AccessLevel;
                            if (arrayTypes.Contains(descriptor.Type))
                            {
                                accessLevel &= AccessLevels.Remove;
                            }

                            descriptors.Add(new KnownPropertyDto
                            {
                                name        = descriptor.Name,
                                type        = (int)descriptor.Type,
                                accessLevel = (int)accessLevel
                            });
                        }

                        packages.Add(new ExtractedPackageDto
                        {
                            id              = package.Id,
                            name            = package.Name,
                            index           = package.Index,
                            type            = (int)package.Type,
                            properties      = properties,
                            knownProperties = descriptors,
                        });
                    }
                    return(packages);
                }
        }
        public HttpResponseMessage ExportProperties(PostedDataDto postedData)
        {
            var result = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new ByteArrayContent(metadataService.ExportMetadata(postedData))
            };

            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "ExportedProperties.xlsx"
            };
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

            return(result);
        }
Exemplo n.º 13
0
        public void RemoveProperties(PostedDataDto postedData)
        {
            string filePath     = metadataConfiguration.GetAbsolutePath(postedData.guid);
            var    tempFilePath = GetTempPath(filePath);

            using (MetadataContext context = new MetadataContext(filePath, postedData.password))
            {
                foreach (var packageInfo in postedData.packages)
                {
                    context.RemoveProperties(packageInfo.id, packageInfo.properties.Select(p => p.name));
                }
                context.Save(tempFilePath);
            }
            DirectoryUtils.MoveFile(tempFilePath, filePath);
        }
 public HttpResponseMessage LoadDocumentDescription(PostedDataDto postedData)
 {
     try
     {
         // return document description
         return(Request.CreateResponse(HttpStatusCode.OK, previewService.LoadDocument(postedData)));
     }
     catch (DocumentProtectedException ex)
     {
         return(Request.CreateResponse(HttpStatusCode.Forbidden, new Resources().GenerateException(ex)));
     }
     catch (Exception ex)
     {
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, new Resources().GenerateException(ex)));
     }
 }
        public HttpResponseMessage LoadDocumentDescription(PostedDataDto postedData)
        {
            string password = "";

            try
            {
                // return document description
                return(Request.CreateResponse(HttpStatusCode.OK, fileService.LoadDocument(postedData)));
            }
            catch (DocumentProtectedException ex)
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden, Resources.GenerateException(ex, password)));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, Resources.GenerateException(ex)));
            }
        }
Exemplo n.º 16
0
 public void CleanMetadata(PostedDataDto postedData)
 {
     UpdateMetadata(postedData, (context) => context.Sanitize());
 }