forked from aloisdg/Doccou
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocument.cs
More file actions
86 lines (80 loc) · 2.54 KB
/
Document.cs
File metadata and controls
86 lines (80 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Doccou.Pcl.Documents;
using Doccou.Pcl.Documents.Archives;
namespace Doccou.Pcl
{
/// <summary>
/// Document is the class wrapping every other class in this library.
/// If you are an user, everything you need come from here.
/// </summary>
public class Document
{
public DocumentType ExtensionType { get; private set; }
public string Extension { get; private set; }
public string FullName { get; private set; }
public string Name { get; private set; }
public string NameWithoutExtension { get; private set; }
public uint? Count { get; private set; }
private readonly Dictionary<string, DocumentType> _extensionsSupported
= new Dictionary<string, DocumentType>
{
{ ".pdf", DocumentType.Pdf },
{ ".docx", DocumentType.Docx },
{ ".pptx", DocumentType.Pptx },
{ ".odt", DocumentType.Odt },
{ ".bmp", DocumentType.Bmp },
{ ".jpg", DocumentType.Jpeg },
{ ".jpeg", DocumentType.Jpeg },
{ ".gif", DocumentType.Gif },
{ ".tiff", DocumentType.Tiff },
{ ".png", DocumentType.Png },
};
/// <remarks>
/// We can't open stream ourself.
/// </remarks>
private Document(string fullName)
{
FullName = fullName;
Name = Path.GetFileName(fullName);
NameWithoutExtension = Path.GetFileNameWithoutExtension(fullName);
Extension = Path.GetExtension(fullName).ToLowerInvariant();
ExtensionType = IsSupported(Extension)
? _extensionsSupported[Extension]
: DocumentType.Unknow;
}
public Document(string fullName, Stream stream)
: this(fullName)
{
Count = !ExtensionType.Equals(DocumentType.Unknow)
? BuildDocument(stream).Count
: 0;
}
public bool IsSupported(string extension)
{
return _extensionsSupported.ContainsKey(extension);
}
// replace with a static dictionary ?
private IDocument BuildDocument(Stream stream)
{
switch (ExtensionType)
{
case DocumentType.Pdf: return new Pdf(stream);
case DocumentType.Docx: return new Docx(stream);
case DocumentType.Pptx: return new Pptx(stream);
case DocumentType.Odt: return new Odt(stream);
case DocumentType.Bmp: return new Bmp(stream);
case DocumentType.Jpeg: return new Jpeg(stream);
case DocumentType.Gif: return new Gif(stream);
case DocumentType.Tiff: return new Tiff(stream);
case DocumentType.Png: return new Png(stream);
default:
throw new NotImplementedException(String.Format(
"{0} is a \"{1}\" and \"{1}\" is not implemented.",
NameWithoutExtension, Extension));
}
}
}
}