public string GetFullPathIfSafe(PluginFolder folder, string relativePath) { string rootFolder = GetPluginFolder(folder); string fullPath = Path.Combine(rootFolder, relativePath); try{ string folderPathName = new DirectoryInfo(rootFolder).FullName; DirectoryInfo currentInfo = new DirectoryInfo(fullPath); while (currentInfo.Parent != null) { if (currentInfo.Parent.FullName == folderPathName) { return(fullPath); } currentInfo = currentInfo.Parent; } } catch { // ignore } return(string.Empty); }
public async Task <HttpResponseMessage> Post(Guid id, string fileName) { try { var folder = PluginFolder.Get(id); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } using (var requestStream = await Request.Content.ReadAsStreamAsync()) using (var fileStream = File.Create(Path.Combine(folder, fileName))) { await requestStream.CopyToAsync(fileStream); } return(new HttpResponseMessage(HttpStatusCode.Created)); } catch (Exception ex) { var response = new HttpResponseMessage(HttpStatusCode.InternalServerError); response.ReasonPhrase = ex.Message; return(response); } }
public string GetPluginFolder(PluginFolder folder) { switch (folder) { case PluginFolder.Root: return(pathRoot); case PluginFolder.Data: return(pathData); default: return(string.Empty); } }
private static string Key(PluginFolder folder, string path) { string prefix = folder switch { PluginFolder.Root => "root/", PluginFolder.Data => "data/", _ => throw new InvalidOperationException($"Invalid folder type {folder}, this is a TweetDuck error.") }; return(prefix + path.Replace('\\', '/').Trim()); } }
/// <summary> /// Helper for creating the plugin folder record for the in-database-path plugins. /// </summary> /// <param name="funcOnSuccess">Executed to acknowledge the result on success, skipped on failure.</param> private static void CreateDefaultFolders_DbPath(Action <PluginFolder> funcOnSuccess) { string sDbFolder = MyPalStorage.DBPath; if (string.IsNullOrEmpty(sDbFolder)) { return; } var pluginfolder = new PluginFolder("Database Plugins", "<DatabaseDir>/Plugins[/*]", new DirectoryInfo(Path.Combine(Path.GetFullPath(sDbFolder), "Plugins")), false); // “Return” the result funcOnSuccess(pluginfolder); }
public PossiblyPluginFileInfo([NotNull] PluginFolder folder, [NotNull] FileInfo file, bool bIsPlugin, [CanBeNull] string sReason) { if (folder == null) { throw new ArgumentNullException("folder"); } if (file == null) { throw new ArgumentNullException("file"); } Folder = folder; File = file; IsPlugin = bIsPlugin; Reason = sReason; }
/// <summary> /// Helper for creating plugin folder records under system special folders. /// Paths: <c>{SpecialFolder}/JetBrains/Omea/Plugins</c>. /// </summary> /// <param name="sFriendlyName"><see cref="Name"/>.</param> /// <param name="sSpecialFolderTitle">Part of the <see cref="Location"/> that identifies the special folder.</param> /// <param name="specialfolder">The system folder to look under.</param> /// <param name="funcOnSuccess">Executed to acknowledge the result on success, skipped on failure.</param> private static void CreateDefaultFolders_SpecialFolder(string sFriendlyName, string sSpecialFolderTitle, Environment.SpecialFolder specialfolder, Action <PluginFolder> funcOnSuccess) { // Folder exists on disk? string sSpecialFolder = Environment.GetFolderPath(specialfolder); if (string.IsNullOrEmpty(sSpecialFolder)) { return; // No such folder } // Create entry var pluginfolder = new PluginFolder(sFriendlyName, string.Format("<{0}>/JetBrains/Omea/Plugins[/*]", sSpecialFolderTitle), new DirectoryInfo(Path.Combine(Path.Combine(Path.Combine(sSpecialFolder, "JetBrains"), "Omea"), "Plugins")), false); // “Return” the result funcOnSuccess(pluginfolder); }
private string ReadFileUnsafe(int token, PluginFolder folder, string path, bool readCached) { string fullPath = GetFullPathOrThrow(token, folder, path); if (readCached && fileCache.TryGetValue(token, folder, path, out string cachedContents)) { return(cachedContents); } try{ return(fileCache[token, folder, path] = File.ReadAllText(fullPath, Encoding.UTF8)); }catch (FileNotFoundException) { throw new FileNotFoundException("File not found."); }catch (DirectoryNotFoundException) { throw new DirectoryNotFoundException("Directory not found."); } }
// Utility methods private string GetFullPathOrThrow(int token, PluginFolder folder, string path) { Plugin?plugin = GetPluginFromToken(token); string fullPath = plugin == null ? string.Empty : plugin.GetFullPathIfSafe(folder, path); if (fullPath.Length == 0) { throw folder switch { PluginFolder.Data => new ArgumentException("File path has to be relative to the plugin data folder."), PluginFolder.Root => new ArgumentException("File path has to be relative to the plugin root folder."), _ => new ArgumentException($"Invalid folder type {folder}, this is a TweetDuck error.") }; } else { return(fullPath); } }
public SimplePluginExample() : base() { Plugin Root = new Plugin(ContentType.Movies); // PluginFolder MyFirstDirectory = Root.CreateDirectory("My First Directory"); PluginFolder MySecondDirectory = Root.CreateDirectory("My Second Directory"); PluginFile MyFirstFile = Root.CreateFile("My File"); // PluginFile MyFirstDirectory_MyFile = MyFirstDirectory.CreateFile("My File"); // MyFirstDirectory.OnSelect += OnSelect; MySecondDirectory.OnSelect += OnSelect; MyFirstFile.OnSelect += OnSelect; MyFirstDirectory_MyFile.OnSelect += OnSelect; // Root.Tread(); }
// Utility methods private string GetFullPathOrThrow(int token, PluginFolder folder, string path) { Plugin plugin = manager.GetPluginFromToken(token); string fullPath = plugin == null ? string.Empty : plugin.GetFullPathIfSafe(folder, path); if (fullPath.Length == 0) { switch (folder) { case PluginFolder.Data: throw new ArgumentException("File path has to be relative to the plugin data folder."); case PluginFolder.Root: throw new ArgumentException("File path has to be relative to the plugin root folder."); default: throw new ArgumentException("Invalid folder type " + folder + ", this is a TweetDuck error."); } } else { return(fullPath); } }
public void Remove(int token, PluginFolder folder, string path) { cache.Remove(token, Key(folder, path)); }
public bool TryGetValue(int token, PluginFolder folder, string path, out string contents) { return(cache.TryGetValue(token, Key(folder, path), out contents)); }
public string this[int token, PluginFolder folder, string path] { set => cache[token, Key(folder, path)] = value;
/// <summary> /// Creates an item that qualifies as an Omea Plugin. /// </summary> public static PossiblyPluginFileInfo CreateYes([NotNull] PluginFolder folder, [NotNull] FileInfo file) { return(new PossiblyPluginFileInfo(folder, file, true, null)); }
/// <summary> /// Creates an item that is NOT an Omea Plugin. /// </summary> public static PossiblyPluginFileInfo CreateNo([NotNull] PluginFolder folder, [NotNull] FileInfo file, [CanBeNull] string sReason) { return(new PossiblyPluginFileInfo(folder, file, false, sReason)); }