Browse Source

Able to read XML data from PSD

Piotr Czajkowski 3 years ago
parent
commit
04665f184c
2 changed files with 45 additions and 3 deletions
  1. 41 1
      PSDText/PSDText.cs
  2. 4 2
      PSDTextTests/PSDTextTests.cs

+ 41 - 1
PSDText/PSDText.cs

@@ -1,6 +1,46 @@
-namespace PSDText
+using System;
+using System.IO;
+using System.Text;
+
+namespace PSDText
 {
     public class PSDText
     {
+        private readonly string _xmlData;
+        private string Readxmpmeta(string path)
+        {
+            var sb = new StringBuilder(1000);
+            using (var sr = new StreamReader(path))
+            {
+                sr.ReadLine(); //skip first line
+                    
+                var line = sr.ReadLine();
+                if (string.IsNullOrWhiteSpace(line))
+                    return string.Empty;
+
+                var read = line.StartsWith("<x:xmpmeta");
+                while (read)
+                {
+                    if (line.StartsWith("</x:xmpmeta>"))
+                        read = false;
+
+                    sb.Append(line);
+
+                    line = sr.ReadLine();
+                    if (string.IsNullOrWhiteSpace(line))
+                        return sb.ToString();
+                }
+            }
+
+            return sb.ToString();
+        }
+
+        public PSDText(string path)
+        {
+            if (!File.Exists(path))
+                throw new Exception($"File {path} doesn't exist!");
+
+            _xmlData = Readxmpmeta(path);
+        }
     }
 }

+ 4 - 2
PSDTextTests/PSDTextTests.cs

@@ -4,10 +4,12 @@ namespace PSDTextTests
 {
     public class PSDTextTests
     {
+        private const string TestFile = "./testFiles/test.psd";
+
         [Fact]
-        public void Test1()
+        public void ReadXMLFromPSD()
         {
-
+            var test = new PSDText.PSDText(TestFile);
         }
     }
 }