Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / Agents / SelectiveUris.cs @ 2b0ec5b8

History | View | Annotate | Download (3 kB)

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

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

    
17
        public ConcurrentDictionary<Uri, List<Uri>> SelectiveUris { get; private set; }
18

    
19
        private ConcurrentDictionary<Uri, List<string>> SelectivePaths { get; set; }
20

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

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

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

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

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

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

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

    
79
    }
80
}