[rygel] core: Mangle invalid characters in CreateItem



commit b9e8bcd491e74e554ff1752e7077f4c6ff7d5997
Author: Jens Georg <mail jensge org>
Date:   Thu Sep 22 15:24:30 2011 +0200

    core: Mangle invalid characters in CreateItem
    
    Do not fail if the upload directory is on a FAT file-system and the
    title contains characters that are invalid on FAT.

 src/rygel/rygel-item-creator.vala |   19 ++++++++++++++++++-
 1 files changed, 18 insertions(+), 1 deletions(-)
---
diff --git a/src/rygel/rygel-item-creator.vala b/src/rygel/rygel-item-creator.vala
index 5ed384b..cabcbe0 100644
--- a/src/rygel/rygel-item-creator.vala
+++ b/src/rygel/rygel-item-creator.vala
@@ -33,6 +33,8 @@ private errordomain Rygel.ItemCreatorError {
 internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
     private static PatternSpec comment_pattern = new PatternSpec ("*<!--*-->*");
 
+    private const string INVALID_CHARS = "/?<>\\:*|\"";
+
     // In arguments
     public string container_id;
     public string elements;
@@ -44,6 +46,7 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
     private ServiceAction action;
     private DIDLLiteWriter didl_writer;
     private DIDLLiteParser didl_parser;
+    private Regex title_regex;
 
     public Cancellable cancellable { get; set; }
 
@@ -54,6 +57,12 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
         this.action = (owned) action;
         this.didl_writer = new DIDLLiteWriter (null);
         this.didl_parser = new DIDLLiteParser ();
+        try {
+            var pattern = "[" + Regex.escape_string (INVALID_CHARS) + "]";
+            this.title_regex = new Regex (pattern,
+                                          RegexCompileFlags.OPTIMIZE,
+                                          RegexMatchFlags.NOTEMPTY);
+        } catch (Error error) { } /* ignore */
     }
 
     public async void run () {
@@ -363,6 +372,14 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
         return true;
     }
 
+    private string mangle_title (string title) throws Error {
+        return this.title_regex.replace_literal (title,
+                                                 -1,
+                                                 0,
+                                                 "_",
+                                                 RegexMatchFlags.NOTEMPTY);
+    }
+
     private async string create_uri (WritableContainer container, string title)
                                     throws Error {
         var dir = yield container.get_writable (this.cancellable);
@@ -373,7 +390,7 @@ internal class Rygel.ItemCreator: GLib.Object, Rygel.StateMachine {
         }
 
         var now = new GLib.DateTime.now_utc ();
-        var file = dir.get_child_for_display_name (title);
+        var file = dir.get_child_for_display_name (this.mangle_title (title));
 
         return file.get_uri () + now.format ("%s");
     }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]