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
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
using System;

namespace SourceBrowser.Site.Utilities
{
    public static class FileUtilities
    {
        /// <summary>
        /// Opens the file specified,
        /// deserializes its content assuming binary serialization
        /// and casts the resulting object to a specified type.
        /// </summary>
        /// <typeparam name="T">Type of the serialized object</typeparam>
        /// <param name="filePath">Path that points to serialized data</param>
        /// <returns>Deserialized object</returns>
        public static T DeserializeData<T>(string filePath)
        {
            using (FileStream fs = new FileStream(filePath, FileMode.Open))
            {
                try
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    return (T)formatter.Deserialize(fs);
                }
                catch (SerializationException e)
                {
                    throw;
                }
            }
        }

        /// <summary>
        /// Serializes specified object into a file, using binary serialization
        /// </summary>
        /// <param name="data">Object to be serialized</param>
        /// <param name="filePath">Path to store serialized data</param>
        public static void SerializeData(object data, string filePath)
        {
            using (FileStream fs = new FileStream(filePath, FileMode.Create))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                try
                {
                    formatter.Serialize(fs, data);
                }
                catch (SerializationException e)
                {
                    throw;
                }
            }
        }

        /// <summary>
        /// Returns true if the file specified exists and was modified within last 120 minutes.
        /// </summary>
        /// <param name="filePath">Path to the file</param>
        /// <returns>true if the file specified exists and was modified within last 120 minutes.</returns>
        public static bool FileIsFresh(string filePath)
        {
            // If file doesn't exist, it's not "fresh" (it needs to be downloaded)
            if (!File.Exists(filePath))
            {
                return false;
            }

            // If file has been modified within last 60 minutes, it is considered "fresh"
            DateTime freshThreshold = DateTime.Now - TimeSpan.FromMinutes(120);
            var lastWriteTime = File.GetLastWriteTimeUtc(filePath);
            var thresholdTime = freshThreshold.ToUniversalTime();
            if (lastWriteTime > thresholdTime)
            {
                return true;
            }

            return false;
        }

    }
}