forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackageSourceFile.cs
More file actions
106 lines (91 loc) · 3.82 KB
/
Copy pathPackageSourceFile.cs
File metadata and controls
106 lines (91 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Hosting;
using System.Xml.Linq;
namespace System.Web.WebPages.Administration.PackageManager
{
/// <summary>
/// Provides an abstraction for reading and writing feeds to disk.
/// Calls to this file must be externally guarded against multiple threads.
/// </summary>
internal class PackageSourceFile : IPackagesSourceFile
{
private const string UrlAttribute = "url";
private const string NameAttribute = "displayname";
private const string FilterPreferredAttribute = "filterpreferred";
private readonly string _fileName;
public PackageSourceFile(string fileName)
{
_fileName = fileName;
}
public void WriteSources(IEnumerable<WebPackageSource> sources)
{
WriteFeeds(sources, () => GetStreamForWrite());
}
public IEnumerable<WebPackageSource> ReadSources()
{
return ReadFeeds(() => GetStreamForRead());
}
public bool Exists()
{
return HostingEnvironment.VirtualPathProvider.FileExists(_fileName);
}
internal static IEnumerable<WebPackageSource> ReadFeeds(Func<Stream> getStream)
{
using (var stream = getStream())
{
var document = XDocument.Load(stream);
var root = document.Root;
return (from element in root.Elements()
select ParsePackageSource(element)).ToList();
}
}
internal static void WriteFeeds(IEnumerable<WebPackageSource> sources, Func<Stream> getStream)
{
var xmlTree = from item in sources
select new XElement("source",
new XAttribute(UrlAttribute, item.Source),
new XAttribute(NameAttribute, item.Name),
new XAttribute(FilterPreferredAttribute, item.FilterPreferredPackages));
using (Stream stream = getStream())
{
new XDocument(new XElement("sources", xmlTree)).Save(stream);
}
}
internal static WebPackageSource ParsePackageSource(XElement element)
{
var urlAttribute = element.Attribute(UrlAttribute);
var nameAttribute = element.Attribute(NameAttribute);
var filterPreferredAttribute = element.Attribute(FilterPreferredAttribute);
// Throw if the file was tampered externally
if (urlAttribute == null || nameAttribute == null)
{
throw new FormatException();
}
Uri feedUrl;
if (!Uri.TryCreate(urlAttribute.Value, UriKind.Absolute, out feedUrl))
{
throw new FormatException();
}
return new WebPackageSource(feedUrl.OriginalString, nameAttribute.Value) { FilterPreferredPackages = filterPreferredAttribute != null && filterPreferredAttribute.Value.AsBool(false) };
}
private Stream GetStreamForRead()
{
var vpp = HostingEnvironment.VirtualPathProvider;
return vpp.GetFile(_fileName).Open();
}
private Stream GetStreamForWrite()
{
string mappedPath = HostingEnvironment.MapPath(_fileName);
if (!File.Exists(_fileName))
{
Directory.CreateDirectory(Path.GetDirectoryName(mappedPath));
return File.Create(mappedPath);
}
return File.Open(mappedPath, FileMode.Truncate);
}
}
}