示例#1
0
        public async Task <IActionResult> FileProxy(string mode, string path, string topic = "", string fileName = "")
        {
            // Special processing for file retrieval of attachments to TFS work items. This is mainly used to return images in item descriptions.
            if (RepositoryTypeHelper.IsMatch(mode, RepositoryTypeNames.VstsWorkItemTracking))
            {
                var model = new TopicViewModel(topic, HttpContext);
                if (SqlDataAccess.CanUseSql)
                {
                    var prefix   = ControllerHelper.GetCurrentDomainPrefix(HttpContext.Request);
                    var settings = await SqlDataAccess.GetSqlRepositorySettingsDynamic(prefix);

                    if (settings == null)
                    {
                        return(NotFound($"Document repository {prefix} does not exist."));
                    }
                    model.SetRootSettingsForRequest(settings);
                }
                await model.LoadData(buildHtml : false, buildToc : true);

                var instance = model.GetSetting <string>(SettingsEnum.VstsInstance);
                var pat      = model.GetSetting <string>(SettingsEnum.VstsPat);
                var stream   = await VstsHelper.GetWorkItemAttachmentStream(path, instance, pat);

                return(File(stream, GetContentTypeFromUrl(fileName), fileName));
            }

            // If we got this far, and the path is a fully qualified URL, then we just retrieve it
            if (path.ToLowerInvariant().StartsWith("http://") || path.ToLowerInvariant().StartsWith("https://"))
            {
                using (var client = new WebClientEx())
                {
                    var data = await client.DownloadDataTaskAsync(new Uri(path));

                    return(File(data, GetContentTypeFromUrl(path), StringHelper.JustFileName(path)));
                }
            }

            // If it is in a VSTS Git repository, we use the API to retrieve it
            if (RepositoryTypeHelper.IsMatch(mode, RepositoryTypeNames.VstsGit))
            {
                if (SqlDataAccess.CanUseSql)
                {
                    var prefix   = ControllerHelper.GetCurrentDomainPrefix(HttpContext.Request);
                    var settings = await SqlDataAccess.GetSqlRepositorySettingsDynamic(prefix);

                    if (settings == null)
                    {
                        return(NotFound($"Document repository {prefix} does not exist."));
                    }
                    var stream = await VstsHelper.GetFileStream(path,
                                                                SettingsHelper.GetSetting <string>(SettingsEnum.VstsInstance, requestRootSettings: settings),
                                                                SettingsHelper.GetSetting <string>(SettingsEnum.VstsProjectName, requestRootSettings: settings),
                                                                SettingsHelper.GetSetting <string>(SettingsEnum.VstsDocsFolder, requestRootSettings: settings),
                                                                SettingsHelper.GetSetting <string>(SettingsEnum.VstsPat, requestRootSettings: settings),
                                                                SettingsHelper.GetSetting <string>(SettingsEnum.VstsApiVersion, requestRootSettings: settings));

                    return(File(stream, GetContentTypeFromUrl(path), StringHelper.JustFileName(path)));
                }
                else
                {
                    var stream = await VstsHelper.GetFileStream(path,
                                                                SettingsHelper.GetSetting <string>(SettingsEnum.VstsInstance),
                                                                SettingsHelper.GetSetting <string>(SettingsEnum.VstsProjectName),
                                                                SettingsHelper.GetSetting <string>(SettingsEnum.VstsDocsFolder),
                                                                SettingsHelper.GetSetting <string>(SettingsEnum.VstsPat),
                                                                SettingsHelper.GetSetting <string>(SettingsEnum.VstsApiVersion));

                    return(File(stream, GetContentTypeFromUrl(path), StringHelper.JustFileName(path)));
                }
            }

            // Otherwise, we got nothing :-)
            return(null);
        }