Ejemplo n.º 1
0
        private ImageRow CreateImageRow(FileInfo fileInfo, string path)
        {
            ImageRow row = new ImageRow();

            row.Creation = System.IO.File.GetCreationTime(path);
            row.Modified = System.IO.File.GetLastWriteTime(path);
            row.FileName = fileInfo.Id;

            string model = "";

            Type fileInfoClass = typeof(MetadataPackage).Assembly.GetType(fileInfo.Class);

            if (((fileInfoClass == typeof(UnresolvedPart)) || fileInfoClass.IsSubclassOf(typeof(UnresolvedPart))) &&
                ((fileInfo.BodyType == ImageRowTypes.CompressedUtf8MarkedString) || (fileInfo.BodyType == ImageRowTypes.Utf8MarkedString)))
            {
                using (StreamReader reader = new StreamReader(path))
                {
                    model = reader.ReadToEnd();
                }
            }
            else if (fileInfoClass.IsSubclassOf(typeof(MetadataPart)))
            {
                if (_settings.Type == ProjectType.Debug)
                {
                    using (StreamReader reader = new StreamReader(path))
                    {
                        model = reader.ReadToEnd();
                    }
                }
                else if (_settings.Type == ProjectType.Xml)
                {
                    MetadataPart mp = null;
                    using (XmlReader xmlReader = XmlReader.Create(path))
                    {
                        mp = System.Windows.Markup.XamlReader.Load(xmlReader) as MetadataPart;
                        //mp.CreateModelFromProperties();
                    }
                    DataSerializer ds = new DataSerializer();
                    model = ds.Serialize(mp.GetModel());
                }
                else
                {
                    throw new InvalidOperationException("Unknown project type");
                }
            }
            else if (fileInfo.BodyType == ImageRowTypes.CompressedImage)
            {
                Image image = new Image();

                if (fileInfo.Items != null)
                {
                    image.Rows = fileInfo.Items.AsParallel().Select(m => CreateImageRow(m, Path.Combine(fileInfo.Path, m.Path))).ToArray();
                }

                using (MemoryStream stream = new MemoryStream())
                {
                    ImageWriter imageWriter = new ImageWriter(stream);
                    imageWriter.WriteImage(image);
                    imageWriter.Dispose();

                    row.BinaryData = stream.GetBuffer();
                    return(row);
                }
            }
            else if ((fileInfoClass == typeof(UnresolvedPart)) || fileInfoClass.IsSubclassOf(typeof(UnresolvedPart)))
            {
                row.BinaryData = System.IO.File.ReadAllBytes(path);
                return(row);
            }
            else
            {
                throw new InvalidOperationException("Unknown fileInfo class");
            }

            UTF8Encoding utf8   = new UTF8Encoding(true);
            MemoryStream memory = new MemoryStream();

            byte[] preamble = utf8.GetPreamble();
            memory.Write(preamble, 0, preamble.Length);
            byte[] content = utf8.GetBytes(model);
            memory.Write(content, 0, content.Length);
            byte[] deflatedBody = memory.ToArray();

            if (fileInfo.BodyType == ImageRowTypes.Utf8MarkedString)
            {
                row.BinaryData = deflatedBody;
            }
            else if (fileInfo.BodyType == ImageRowTypes.CompressedUtf8MarkedString)
            {
                MemoryStream  compressedStream = new MemoryStream();
                DeflateStream stream           = new DeflateStream(compressedStream, CompressionMode.Compress);
                stream.Write(deflatedBody, 0, deflatedBody.Length);
                stream.Close();
                row.BinaryData = compressedStream.ToArray();
            }
            else
            {
                throw new InvalidOperationException("Unknown File Info Body Type");
            }

            return(row);
        }