下面就是結構化存儲的實現FileSystemFFS
FileSystemFFS.cs
1using System;
2using System.IO;
3using System.Web;
4using ExpertLib;
5using ExpertLib.IO;
6using ExpertLib.IO.Storage;
7using System.Text.RegularExpressions;
8
9namespace Step1.WebFileSystem
10{
11 public class FileSystemFFS:FileSystem
12 {
13 private Storage ffsFile=null;
14 private string filePath, ffsPath;
15 private bool writeError = false;
16 public FileSystemFFS(string path, Handler handle)
17 : base(path, handle)
18 {
19 this.ffsPath = httpContext.Server.MapPath(Regex.Replace(path,handle.Pattern,handle.SavePath));
20 this.filePath = Regex.Replace(path, handle.Pattern, handle.SaveName).Replace('/', '_');
21 }
22 private void Open()
23 {
24 ffsFile = StorageFile.OpenStorageFile(this.ffsPath);
25 }
26 public override bool Exists()
27 {
28 if (ffsFile == null)
29 {
30 FileInfo fileInfo = new FileInfo(this.ffsPath);
31 if (!fileInfo.Exists) { return false; }
32 Open();
33 }
34 return ffsFile.IsElementExist(this.filePath);
35 }
36 public override DateTime GetLastWriteTime()
37 {
38 if (ffsFile == null)
39 {
40 FileInfo fileInfo = new FileInfo(this.ffsPath);
41 if (!fileInfo.Exists) { return DateTime.Now; }
42 Open();
43 }
44 List<StgElementInfo> elementsInfo = ffsFile.GetChildElementsInfo();
45 for (int i = elementsInfo.Count - 1; i <= 0; i--)
46 {
47 if (elementsInfo[i].Name == filePath)
48 {
49 return elementsInfo[i].LastModifyTime;
50 }
51 }
52 return DateTime.Now;
53 }
54 public override Stream GetWriter()
55 {
56 Close();
57 if (ffsFile == null)
58 {
59 if (!File.Exists(this.ffsPath))
60 {
61 CreateFolder(this.ffsPath,false);
62 ffsFile = StorageFile.CreateStorageFile(this.ffsPath,StorageCreateMode.Create,StorageReadWriteMode.ReadWrite,StorageShareMode.ShareDenyWrite,StorageTransactedMode.Direct);
63 }
64 else
65 {
66 Open();
67 }
68 }
69 stream = (Stream)ffsFile.CreateStream(filePath);
70 if (stream == null)
71 {
72 writeError = true;
73 return base.GetWriter();
74 }
75 if (this.handle.UseGzip)
76 {
77 return (Stream)(new GZipOutputStream(stream));
78 }
79 else
80 {
81 return stream;
82 }
83 }
84 public override Stream GetReader()
85 {
86 Close();
87 if (ffsFile == null)
88 {
89 FileInfo fileInfo = new FileInfo(this.ffsPath);
90 if (!fileInfo.Exists) { return null; }
91 Open();
92 }
93 return stream = (Stream)ffsFile.OpenStream(filePath);
94 }
95 public override void TransmitFile()
96 {
97 if (writeError)
98 {
99 base.TransmitFile();
100 }
101 else
102 {
103 this.TransmitStream(false);
104 }
105 }
106 public override void Dispose()
107 {
108 Close();
109 if (ffsFile != null)
110 {
111 ffsFile.Dispose();
112 ffsFile = null;
113 }
114 }
115 }
116}
117