| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Linq; |
| | | 5 | | using Newtonsoft.Json; |
| | | 6 | | using ReFlex.Core.Common.Util; |
| | | 7 | | |
| | | 8 | | namespace ReFlex.Sensor.EmulatorModule |
| | | 9 | | { |
| | | 10 | | public static class RecordingUtils |
| | | 11 | | { |
| | | 12 | | public const string SavePath = "wwwroot/recordings/"; |
| | | 13 | | public const string ConfigFile = "config.json"; |
| | | 14 | | |
| | | 15 | | public static bool DeleteRecording(string name) |
| | 26 | 16 | | { |
| | 26 | 17 | | if (!Directory.Exists(SavePath)) |
| | 2 | 18 | | { |
| | 2 | 19 | | Directory.CreateDirectory(SavePath); |
| | 2 | 20 | | return false; |
| | | 21 | | } |
| | | 22 | | |
| | 24 | 23 | | var recordings = Directory.GetDirectories(SavePath); |
| | | 24 | | |
| | 24 | 25 | | var numDirectoriesDeleted = 0; |
| | | 26 | | |
| | 72 | 27 | | foreach (var dir in recordings) |
| | 0 | 28 | | { |
| | 0 | 29 | | var cfgFile = Path.Combine(dir, ConfigFile); |
| | | 30 | | |
| | 0 | 31 | | if (!File.Exists(cfgFile)) |
| | 0 | 32 | | continue; |
| | | 33 | | |
| | 0 | 34 | | var cfgObj = File.ReadAllText(cfgFile); |
| | | 35 | | |
| | 0 | 36 | | var cfg = JsonConvert.DeserializeObject<StreamParameter>(cfgObj); |
| | | 37 | | |
| | 0 | 38 | | if (!Equals(cfg.Name, name)) |
| | 0 | 39 | | continue; |
| | | 40 | | |
| | 0 | 41 | | var deleted = DeleteRecordingDirectory(dir); |
| | 0 | 42 | | if (deleted) |
| | 0 | 43 | | numDirectoriesDeleted += 1; |
| | 0 | 44 | | } |
| | | 45 | | |
| | 24 | 46 | | return numDirectoriesDeleted > 0; |
| | 26 | 47 | | } |
| | | 48 | | |
| | | 49 | | public static int DeleteAllRecordings() |
| | 0 | 50 | | { |
| | 0 | 51 | | if (!Directory.Exists(SavePath)) |
| | 0 | 52 | | { |
| | 0 | 53 | | Directory.CreateDirectory(SavePath); |
| | 0 | 54 | | return 0; |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | var recordings = Directory.GetDirectories(SavePath); |
| | | 58 | | |
| | 0 | 59 | | return recordings. |
| | 0 | 60 | | // find all directories with config file |
| | 0 | 61 | | Where(dir => File.Exists(Path.Combine(dir, ConfigFile))). |
| | 0 | 62 | | // delete directories and select result of delete Method |
| | 0 | 63 | | Select(DeleteRecordingDirectory). |
| | 0 | 64 | | // count number of successful deletions |
| | 0 | 65 | | Count(deleted => deleted); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | public static IEnumerable<StreamParameter> RetrieveConfigurations() |
| | 22 | 69 | | { |
| | 22 | 70 | | if(!Directory.Exists(SavePath)) |
| | 0 | 71 | | return new List<StreamParameter>(); |
| | | 72 | | |
| | 22 | 73 | | var recordings = Directory.GetDirectories(SavePath); |
| | | 74 | | |
| | 22 | 75 | | return recordings. |
| | 22 | 76 | | // Select config file path |
| | 22 | 77 | | Select(dir => Path.Combine(dir, ConfigFile)). |
| | 22 | 78 | | // filter all directories that do not contain config file |
| | 22 | 79 | | Where(File.Exists) |
| | 22 | 80 | | // load and deserialize config file |
| | 44 | 81 | | .Select(file => JsonConvert.DeserializeObject<StreamParameter>(File.ReadAllText(file))); |
| | 22 | 82 | | } |
| | | 83 | | |
| | | 84 | | public static string[] GetFrames(string name) |
| | 52 | 85 | | { |
| | 52 | 86 | | if(!Directory.Exists(SavePath)) |
| | 0 | 87 | | return new string[0]; |
| | | 88 | | |
| | 52 | 89 | | var recordings = Directory.GetDirectories(SavePath); |
| | | 90 | | |
| | 52 | 91 | | var settings = recordings. |
| | 52 | 92 | | // Select tuple containing directory and config file path |
| | 52 | 93 | | Select(dir => Tuple.Create(dir, Path.Combine(dir, ConfigFile))). |
| | 52 | 94 | | // filter all directories that do not contain config file |
| | 52 | 95 | | Where(tpl => File.Exists(tpl.Item2)) |
| | 52 | 96 | | // load and deserialize config file |
| | 52 | 97 | | .Select(tpl => Tuple.Create(tpl.Item1, JsonConvert.DeserializeObject<StreamParameter>(File.ReadAllText(t |
| | 52 | 98 | | // filter configs with matching name |
| | 52 | 99 | | .Where(tpl => Equals(tpl.Item2.Name, name)). |
| | 52 | 100 | | // return first item (if there are matching elements) |
| | 52 | 101 | | FirstOrDefault(); |
| | | 102 | | |
| | 52 | 103 | | var selectedRecordingsDir = settings?.Item1; |
| | | 104 | | |
| | 52 | 105 | | if (settings?.Item2 == null || selectedRecordingsDir == null || !Directory.Exists(selectedRecordingsDir)) |
| | 0 | 106 | | return new string[0]; |
| | | 107 | | |
| | | 108 | | // File-system enumeration order is unspecified. The recorder names frames |
| | | 109 | | // by their sequence number; sort numerically, including beyond seven digits. |
| | | 110 | | // Storage format is independent of pixel format. Support both PNG |
| | | 111 | | // and JPEG, including 16-bit streams recorded as lossy JPEG. |
| | 52 | 112 | | var frames = Directory.GetFiles(selectedRecordingsDir, "*.png"); |
| | 52 | 113 | | if (frames.Length == 0) |
| | 8 | 114 | | frames = Directory.GetFiles(selectedRecordingsDir, "*.jpg"); |
| | | 115 | | |
| | 52 | 116 | | return frames |
| | 22 | 117 | | .OrderBy(file => ulong.Parse(Path.GetFileNameWithoutExtension(file), System.Globalization.CultureInfo.In |
| | 52 | 118 | | .ToArray(); |
| | 52 | 119 | | } |
| | | 120 | | |
| | | 121 | | private static bool DeleteRecordingDirectory(string directory) |
| | 0 | 122 | | { |
| | 0 | 123 | | if (Directory.Exists(directory)) |
| | 0 | 124 | | Directory.Delete(directory, true); |
| | | 125 | | |
| | 0 | 126 | | return !Directory.Exists(directory); |
| | 0 | 127 | | } |
| | | 128 | | } |
| | | 129 | | } |