PageRenderTime 51ms CodeModel.GetById 12ms app.highlight 28ms RepoModel.GetById 2ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/FileSystem/File.cs

#
C# | 401 lines | 222 code | 43 blank | 136 comment | 9 complexity | bb0a2ecf38a0b581b7387d6e917da8ea MD5 | raw file
  1using System;
  2using System.Linq;
  3using System.IO;
  4using System.Web;
  5using BlogEngine.Core.Providers;
  6
  7namespace BlogEngine.Core.FileSystem
  8{
  9    /// <summary>
 10    /// The file is a virtual file that is loaded into memory.
 11    /// </summary>
 12    /// <remarks>
 13    ///     Not to be confused with System.IO.File
 14    /// </remarks>
 15    public partial class File : BusinessBase<File, string>, IComparable<File>
 16    {
 17
 18        #region Constants and Fields
 19        /// <summary>
 20        /// the binary of the file
 21        /// </summary>
 22        private byte[] contents;
 23        
 24        /// <summary>
 25        /// the file size, in raw long format
 26        /// </summary>
 27        private long fileSize;
 28
 29        /// <summary>
 30        /// the parent directory of the file
 31        /// </summary>
 32        private Directory parentDirectory;
 33
 34        /// <summary>
 35        /// the name of the file without the path
 36        /// </summary>
 37        private string name;
 38
 39        /// <summary>
 40        /// the full path of the file, internal field only, use file path for external calls. reduces security concerns
 41        /// while outside of the buisness layer
 42        /// </summary>
 43        internal string fullpath;
 44
 45        /// <summary>
 46        /// the relative file path to the blog file container
 47        /// </summary>
 48        private string filepath;
 49
 50        /// <summary>
 51        /// list of valid image extensions
 52        /// </summary>
 53        private string[] ImageExtensnios = { ".jpg", ".png", ".jpeg", ".tiff", ".gif", ".bmp" };
 54        #endregion
 55
 56        #region Properties
 57        /// <summary>
 58        /// Gets the File Contents, Lazy loaded
 59        /// </summary>
 60        /// <remarks>
 61        /// set accessor set to internal
 62        /// </remarks>
 63        public byte[] FileContents
 64        {
 65            get
 66            {
 67                if (this.contents == null)
 68                    FileContents = BlogService.GetFileContents(this).FileContents;
 69                return this.contents;
 70            }
 71            internal set
 72            {
 73                base.SetValue("FileContents", value, ref this.contents);
 74            }
 75        }
 76
 77        /// <summary>
 78        /// gets the file size, in raw long
 79        /// </summary>
 80        /// <remarks>
 81        /// set accessor set to internal
 82        /// </remarks>
 83        public long FileSize
 84        {
 85            get
 86            {
 87                return this.fileSize;
 88            }
 89            internal set
 90            {
 91                base.SetValue("FileSize", value, ref this.fileSize);
 92            }
 93        }
 94
 95        /// <summary>
 96        /// gets the parent directory, uses Lazy loading
 97        /// </summary>
 98        /// <remarks>
 99        /// set accessor set to internal
100        /// </remarks>
101        public Directory ParentDirectory
102        {
103            get
104            {
105                if (this.parentDirectory == null)
106                {
107                    var parent = this.FullPath.Contains("/") ? this.FullPath.Substring(0,this.FullPath.LastIndexOf("/")) : this.FullPath;
108                    ParentDirectory = BlogService.GetDirectory(parent);
109                }
110                return this.parentDirectory;
111            }
112            internal set
113            {
114                base.SetValue("ParentDirectory", value, ref this.parentDirectory);
115            }
116        }
117
118        /// <summary>
119        /// gets the file name
120        /// </summary>
121        /// <remarks>
122        /// set accessor set to the internal
123        /// </remarks>
124        public string Name
125        {
126            get
127            {
128                return this.name;
129            }
130            internal set
131            {
132                base.SetValue("Name", value, ref this.name);
133            }
134        }
135
136        /// <summary>
137        /// gets the full path, internale set. To change the path use rename methods
138        /// </summary>
139        internal string FullPath
140        {
141            get
142            {
143                return this.fullpath;
144            }
145            set
146            {
147                base.SetValue("FullPath", value, ref this.fullpath);
148            }
149        }
150
151        /// <summary>
152        /// gets the relative path to the file
153        /// </summary>
154        /// <remarks>
155        /// set accessor set to internal
156        /// </remarks>
157        public string FilePath
158        {
159            get
160            {
161                return this.filepath;
162            }
163            internal set
164            {
165                base.SetValue("FilePath", value, ref this.filepath);
166            }
167        }
168
169        /// <summary>
170        /// gets the file extension
171        /// </summary>
172        public string Extension
173        {
174            get
175            {
176                return Path.GetExtension(Name);
177            }
178        }
179
180        /// <summary>
181        /// gets the safe file name, URL encoded version of this.FilePath
182        /// </summary>
183        public string SafeFilePath
184        {
185            get
186            {
187                return HttpUtility.UrlEncode(this.FilePath);
188            }
189        }
190
191        /// <summary>
192        /// gets the file size in string formated
193        /// </summary>
194        public string FileSizeFormat
195        {
196            get
197            {
198                return SizeFormat(this.FileSize, "N");
199            }
200        }
201
202        /// <summary>
203        /// gets the full file description of name and filesize (formated)
204        /// </summary>
205        public string FileDescription
206        {
207            get
208            {
209                return string.Format("{0} ({1})", this.Name, SizeFormat(this.FileSize, "N"));
210            }
211
212        }
213
214        /// <summary>
215        /// gets the full download path to the file, using the file handler
216        /// </summary>
217        public string FileDownloadPath
218        {
219            get
220            {
221                return string.Format("{0}FILES{1}.axdx", Utils.RelativeWebRoot, this.SafeFilePath);
222            }
223        }
224
225        /// <summary>
226        /// gets the last access time for the file
227        /// </summary>
228        /// <remarks>
229        /// set accessor set to internal
230        /// </remarks>
231        public DateTime LastAccessTime { get; internal set; }
232
233        /// <summary>
234        /// valdidates if this object is an image
235        /// </summary>
236        public bool IsImage
237        {
238            get
239            {
240                return ImageExtensnios.Any(x => x.ToLower() == this.Extension.ToLower());
241            }
242        }
243
244        /// <summary>
245        /// converts the object to the Image object and disposes of the original object. An exception will be thrown if the image is not of a file type.
246        /// </summary>
247        /// <remarks>
248        /// always compare the IsImage flag first before attempting a direct call to AsImage
249        /// </remarks>
250        public Image AsImage
251        {
252            get
253            {
254                var img = new Image(this);
255                this.Dispose();
256                return img;
257            }
258        }
259
260        #endregion
261
262        #region Methods
263
264        /// <summary>
265        /// deletes the current file object from the storage container
266        /// </summary>
267        public void Delete()
268        {
269            BlogService.DeleteFile(this.FullPath);
270            this.Dispose();
271        }
272
273        /// <summary>
274        /// renames the current file
275        /// </summary>
276        /// <param name="NewName">the new name of the file</param>
277        /// <returns>the new file objbect</returns>
278        /// <remarks>
279        /// methods performs an upload, then deletes itself. The result will dispose of the original object
280        /// </remarks>
281        public File Rename(string NewName)
282        {
283            var newFile = BlogService.UploadFile(this.FileContents, NewName, this.ParentDirectory);
284            this.Delete();
285            this.Dispose();
286            return newFile;
287        }
288
289        /// <summary>
290        /// moves the file to a new directory & new name
291        /// </summary>
292        /// <param name="NewName">the new name</param>
293        /// <param name="NewBaseDirectory">the new directory</param>
294        /// <returns>
295        /// the new file object
296        /// </returns>
297        /// <remarks>
298        /// methods perfoms an upload, the deletes iteself. The result will dispose of the original object
299        /// </remarks>
300        public File MoveFile(string NewName, Directory NewBaseDirectory)
301        {
302            var newFile = BlogService.UploadFile(this.FileContents, NewName, NewBaseDirectory, true);
303            this.Delete();
304            this.Dispose();
305            return newFile;
306        }
307
308        /// <summary>
309        /// copys a file from one directory to another
310        /// </summary>
311        /// <param name="NewBaseDirectory">the new directory</param>
312        /// <returns>the new file object</returns>
313        /// <remarks>
314        /// both object will be maintained after the copy. 
315        /// </remarks>
316        public File CopyFile(Directory NewBaseDirectory)
317        {
318            return BlogService.UploadFile(this.FileContents, this.Name, NewBaseDirectory, true);
319        }
320
321        /// <summary>
322        /// copys a file from one directory to another with a new name
323        /// </summary>
324        /// <param name="NewName">the new name</param>
325        /// <param name="NewBaseDirectory">the new directory</param>
326        /// <returns>the new file object</returns>
327        /// <remarks>
328        /// both object will be maintained after the copy. 
329        /// </remarks>
330        public File CopyFile(string NewName, Directory NewBaseDirectory)
331        {
332            return BlogService.UploadFile(this.FileContents, NewName, NewBaseDirectory, true);
333        }
334
335
336        #region Unimplemented Methods
337        protected override void ValidationRules()
338        {
339            throw new NotImplementedException();
340        }
341
342        protected override void DataUpdate()
343        {
344            throw new NotImplementedException();
345        }
346
347        protected override File DataSelect(string id)
348        {
349            throw new NotImplementedException();
350        }
351
352        protected override void DataInsert()
353        {
354            throw new NotImplementedException();
355        }
356
357        protected override void DataDelete()
358        {
359            throw new NotImplementedException();
360        }
361
362        public int CompareTo(File other)
363        {
364            throw new NotImplementedException();
365        }
366        #endregion
367
368        /// <summary>
369        /// formats a file to a predetermided size
370        /// </summary>
371        /// <param name="size">the file size as a float</param>
372        /// <param name="formatString">the numeric format string</param>
373        /// <returns>the formated string</returns>
374        private static string SizeFormat(float size, string formatString)
375        {
376            if (size < 1024)
377            {
378                return string.Format("{0} bytes", size.ToString(formatString));
379            }
380
381            if (size < Math.Pow(1024, 2))
382            {
383                return string.Format("{0} kb", (size / 1024).ToString(formatString));
384            }
385
386            if (size < Math.Pow(1024, 3))
387            {
388                return string.Format("{0} mb", (size / Math.Pow(1024, 2)).ToString(formatString));
389            }
390
391            if (size < Math.Pow(1024, 4))
392            {
393                return string.Format("{0} gb", (size / Math.Pow(1024, 3)).ToString(formatString));
394            }
395
396            return size.ToString(formatString);
397        }
398        #endregion
399
400    }
401}