文件查看器允许平台自定义上传文件和 URL 的显示。IFileViewer 类型定义查看器应处理的 URL 或文件扩展名以及应显示的输出。Limyee 电商平台附带了各种文件查看器,包括视频,音频,URL,Office文档,PDF,文字和源代码等。
为什么我应该创建一个文件查看器?
文件查看器可以内联显示文件的内容,而不是提供下载文件的链接。例如,Youtube 文件查看器不是显示指向 Youtube 视频的超链接,而是以内联方式显示该视频,并允许在不离开平台的情况下播放该视频。
创建文件查看器
要添加对文件查看器的支持,请实现 IFileViewer 插件类型(可在 Limyee.Api.dll 中找到)。IFileViewer 接口扩展了 IPlugin 以添加支持自定义文件查看器的功能。
首先,实现“SupportedUrlPattern”、“SupportedFileExtensions”和“DefaultOrderNumber”属性。我们的示例文件查看器将允许在平台中查看Vine嵌入URL。由于我们不支持使用此文件查看器查看上载的文件,因此 SupportedFileExtensions 属性将返回一个空集合。属性使用正则表达式将文件查看器限制为包含 vine.co 域的 URL。DefaultOrderNumber 定义根据“SupportedFileExtensions”和“SupportedUrlPattern”属性检查文件查看器的顺序。如果两个不同的文件查看器都能够处理相同的文件或 Url,则具有较低顺序号的文件查看器将优先。
public int DefaultOrderNumber { get { return 100; } } public string SupportedUrlPattern { get { return @"https?://vine\.co/"; } } public string[] SupportedFileExtensions { get { return new string[0]; } }
接下来,实现 GetMediaType 方法。有两种方法,一种用于查看 Url,另一种用于文件。示例中,我们的文件查看器不支持查看文件,因此我们可以在 GetMediaType(ICentralizedFile file, IFileViewerOptions options) 中向平台抛出 FileViewerNotSupportedException 来指示它。示例也不支持 URL预览,使用 GetMediaType(Uri url, IFileViewerOptions options) 方法在预览 Url 时会抛出 FileViewerNotSupportedException,该方法仅返回视频。
public FileViewerMediaType GetMediaType(ICentralizedFile file, IFileViewerOptions options) { throw new FileViewerNotSupportedException(); } public FileViewerMediaType GetMediaType(Uri url, IFileViewerOptions options) { if (options.ViewType == FileViewerViewType.Preview) throw new FileViewerNotSupportedException(); return FileViewerMediaType.Video; }
最后,实现 Render 方法。同样,由于我们不支持在示例中查看文件,因此此方法将引发 FileViewerNotSupportedException。将 Url 作为参数接收的 Render 方法将具有两个代码路径。我们的示例不提供特定于 Vine 的预览图像,因此指定直接返回 FileViewerNotSupportedException。该平台将提供默认图像预览。观看视频时,该方法将返回一个带有 vine Url 的 IFrame,该 URL 将在内容中以内联方式显示 vine 视频。
public string Render(Uri url, IFileViewerOptions options) { var uiApi = Limyee.Extensibility.Apis.Get<IUI>(); if (options.ViewType == FileViewerViewType.Preview) { throw new FileViewerNotSupportedException(); } else { var width = options.Width.HasValue ? options.Width.Value : 0; var height = options.Height.HasValue ? options.Height.Value : 0; return string.Format("<iframe src=\"{0}\" width=\"{1}\" height=\"{2}\" frameborder=\"0\" scrolling=\"no\" />" , Globals.EnsureHtmlEncoded(url.ToString()), width, height); } }
源码
启用完成的插件后,vine.co 嵌入 URL 现在将内联显示在您的内容中。以下是完整的类,包括 IPlugin 实现。
using System; using System.Web; using Limyee.DynamicConfiguration.Components; using Limyee.Components; using Limyee.Extensibility.Api.Version1; using Limyee.Extensibility.Storage.Version1; using Limyee.Extensibility.UI.Version1; using Limyee.Extensibility.Version1; namespace Examples { public class VineEmbedFileViewer : Limyee.Extensibility.UI.Version1.IFileViewer { #region IPlugin Members public string Name { get { return "Vine 文件查看器"; } } public string Description { get { return "允许嵌入 Vine 视频"; } } public void Initialize() { } #endregion #region IFileViewer Members public int DefaultOrderNumber { get { return 100; } } public string SupportedUrlPattern { get { return @"http[s]?://vine\.co/"; } } public string[] SupportedFileExtensions { get { return new string[0]; } } public string Render(ICentralizedFile file, IFileViewerOptions options) { throw new FileViewerNotSupportedException(); } public string Render(Uri url, IFileViewerOptions options) { var uiApi = Limyee.Extensibility.Apis.Get<IUI>(); if (options.ViewType == FileViewerViewType.Preview) { throw new FileViewerNotSupportedException(); } else { var width = options.Width.HasValue ? options.Width.Value : 0; var height = options.Height.HasValue ? options.Height.Value : 0; return string.Format("<iframe src=\"{0}\" width=\"{1}\" height=\"{2}\" frameborder=\"0\" scrolling=\"no\" />" , Globals.EnsureHtmlEncoded(url.ToString()), width, height); } } public FileViewerMediaType GetMediaType(ICentralizedFile file, IFileViewerOptions options) { throw new FileViewerNotSupportedException(); } public FileViewerMediaType GetMediaType(Uri url, IFileViewerOptions options) { if (options.ViewType == FileViewerViewType.Preview) throw new FileViewerNotSupportedException(); return FileViewerMediaType.Video; } #endregion } }