Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / SelectiveUris.cs @ 2341c603

History | View | Annotate | Download (2.8 kB)

1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel.Composition;
4
using System.IO;
5
using System.Linq;
6
using System.Text;
7
using Pithos.Interfaces;
8
using Pithos.Network;
9

    
10
namespace Pithos.Core.Agents
11
{
12
    [Export(typeof(Selectives))]
13
    public class Selectives
14
    {
15

    
16
        public Dictionary<Uri, List<Uri>> SelectiveUris { get; private set; }
17

    
18
        private Dictionary<Uri, List<string>> SelectivePaths { get; set; }
19

    
20
        public Selectives()
21
        {
22
            SelectiveUris = new Dictionary<Uri, List<Uri>>();
23
            SelectivePaths = new Dictionary<Uri, List<string>>();
24
        }
25

    
26
        public void SetSelectedUris(AccountInfo account,List<Uri> uris)
27
        {
28
            SelectiveUris[account.AccountKey] = uris;
29
            SelectivePaths[account.AccountKey] = UrisToFilePaths(account,uris);
30
        }
31

    
32
        public bool IsSelected(ObjectInfo info)
33
        {
34
            List<Uri> filterUris;
35
            return !SelectiveUris.TryGetValue(info.AccountKey, out filterUris) 
36
                || filterUris.Count ==0
37
                || filterUris.Any(f => info.Uri.IsAtOrDirectlyBelow(f));
38
        }
39

    
40
        public bool IsSelected(AccountInfo account,FileSystemInfo info)
41
        {
42
            return IsSelected(account,info.FullName);
43
        }
44

    
45
        public bool IsSelected(AccountInfo account, string fullPath)
46
        {
47
            List<string> paths;
48
            return !SelectivePaths.TryGetValue(account.AccountKey, out paths)
49
                || paths.Count == 0
50
                || paths.Any(fullPath.IsAtOrDirectlyBelow);
51
        }
52

    
53
        /// <summary>
54
        /// Return a list of absolute filepaths from a list of Uris
55
        /// </summary>
56
        /// <param name="uris"></param>
57
        /// <returns></returns>
58
        private List<string> UrisToFilePaths(AccountInfo account,IEnumerable<Uri> uris)
59
        {
60
            if (uris == null)
61
                return new List<string>();
62

    
63
            var accountPath = account.AccountPath;
64
            var storageUrl = account.StorageUri.ToString();
65
            var own = (from uri in uris
66
                       where uri.ToString().StartsWith(storageUrl)
67
                       let relativePath = account.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath()
68
                       //Trim the account name
69
                       select Path.Combine(accountPath, relativePath.After(account.UserName + '\\'))).ToList();
70
            var others = (from uri in uris
71
                          where !uri.ToString().StartsWith(storageUrl)
72
                          let relativePath = account.StorageUri.MakeRelativeUri(uri).RelativeUriToFilePath()
73
                          //Trim the account name
74
                          select Path.Combine(accountPath, "others-shared", relativePath)).ToList();
75
            return own.Union(others).ToList();
76
        }
77

    
78
    }
79
}