Last active
August 29, 2015 14:18
-
-
Save tanaka-takayoshi/90e574398d3f5998b0bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FeedController : Controller | |
{ | |
// GET: Feed | |
public ActionResult Index() | |
{ | |
// Webサイト上の PackageBin フォルダ以下にある vsix ファイル一覧を取得 | |
var entry = Directory.EnumerateFiles(Server.MapPath("./PackageBin"), "*.vsix") | |
.Select(Parse) | |
.Where(e => e != null) | |
.ToArray(); | |
var feed = new feed | |
{ | |
entry = entry, | |
//ここのIDは適当に | |
id = "uuid:bcsdsdd5-9999-9999-9999-7d9e16652433;id=1", | |
title = new feedTitle {type = "text"}, | |
updated = DateTime.Now | |
}; | |
return new XmlResult(feed); | |
} | |
private feedEntry Parse(string path) | |
{ | |
//VSIXはZIPで圧縮してあるので解凍して中身を触る | |
using (var archive = ZipFile.OpenRead(path)) | |
{ | |
//ほしいのはzipの中にある extension.manifest というファイル | |
var entry = archive.Entries.FirstOrDefault(f => f.Name == "extension.vsixmanifest"); | |
if (entry == null) | |
return null; | |
// XmlSerializer を使って中身のXMLをデシリアライズ | |
var xs = new XmlSerializer(typeof(PackageManifest)); | |
using (var stream = entry.Open()) | |
{ | |
var manifest = (PackageManifest)xs.Deserialize(stream); | |
//VSIX1つに対応するAtomフィードのfeedEntryクラスを作成 | |
return new feedEntry | |
{ | |
author = new feedEntryAuthor | |
{ | |
name = manifest.Metadata.Identity.Publisher | |
}, | |
//カテゴリはVSIXの外で管理するけど、面倒なので決め打ち | |
category = new feedEntryCategory | |
{ | |
term = "roslyn" | |
}, | |
//このVSIXのDL先。サーバー上のファイルをそのままDLしてもらう。 | |
content = new feedEntryContent | |
{ | |
src = $"PackageBin/{Path.GetFileName(path)}", | |
type = "application/octet-stream" | |
}, | |
id = manifest.Metadata.Identity.Id, | |
//作成・更新時刻はファイルの更新日時で代用 | |
published = entry.LastWriteTime.LocalDateTime, | |
summary = new feedEntrySummary | |
{ | |
type = "text", | |
Value = manifest.Metadata.Description | |
}, | |
title = new feedEntryTitle | |
{ | |
type = "text", | |
Value = manifest.Metadata.DisplayName | |
}, | |
updated = entry.LastWriteTime.LocalDateTime, | |
Vsix = new Vsix | |
{ | |
Id = manifest.Metadata.Identity.Id, | |
Version = manifest.Metadata.Identity.Version | |
} | |
}; | |
} | |
} | |
} | |
} | |
//オブジェクトをXMLとして返すためのクラス | |
public class XmlResult : ActionResult | |
{ | |
private readonly object objectToSerialize; | |
public XmlResult(object objectToSerialize) | |
{ | |
this.objectToSerialize = objectToSerialize; | |
} | |
public object ObjectToSerialize => objectToSerialize; | |
public override void ExecuteResult(ControllerContext context) | |
{ | |
if (objectToSerialize != null) | |
{ | |
context.HttpContext.Response.Clear(); | |
var xs = new XmlSerializer(objectToSerialize.GetType()); | |
context.HttpContext.Response.ContentType = "text/xml"; | |
xs.Serialize(context.HttpContext.Response.Output, objectToSerialize); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment