67 lines
2.5 KiB
C#
67 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using isnd.Entities;
|
|
using Microsoft.AspNetCore.Http;
|
|
using isn.abst;
|
|
using System.IO;
|
|
|
|
namespace isnd.Controllers
|
|
{
|
|
|
|
public partial class PackagesController
|
|
{
|
|
// TODO [Authorize(Policy = IsndConstants.RequireValidApiKey)]
|
|
[HttpPut("~" + Constants.ApiVersionPrefix + ApiConfig.Package)]
|
|
public async Task<IActionResult> Put()
|
|
{
|
|
try
|
|
{
|
|
var clientVersionId = Request.Headers["X-NuGet-Client-Version"];
|
|
string apiKey = Request.Headers["X-NuGet-ApiKey"][0];
|
|
ViewData["versionId"] = typeof(PackagesController).Assembly.FullName;
|
|
var files = new List<string>();
|
|
ViewData["files"] = files;
|
|
|
|
var clearKey = protector.Unprotect(apiKey);
|
|
var dbApiKey = dbContext.ApiKey.SingleOrDefault(k => k.Id == clearKey);
|
|
if (dbApiKey == null)
|
|
{
|
|
logger.LogError("403 : no api-key");
|
|
return Unauthorized();
|
|
}
|
|
foreach (IFormFile file in Request.Form.Files)
|
|
{
|
|
FileInfo inputFileInfo = new FileInfo(file.Name);
|
|
switch (inputFileInfo.Extension)
|
|
{
|
|
case "nupkg":
|
|
case "nupkgs":
|
|
var libVersion = await packageManager.PutPackageAsync(inputFileInfo.Extension, file.OpenReadStream(), dbApiKey.UserId);
|
|
logger.LogInformation($"new package : {libVersion.PackageId} {libVersion.NugetLink}");
|
|
break;
|
|
default:
|
|
logger.LogInformation($"file extension is not supported : {inputFileInfo.Extension}");
|
|
break;
|
|
}
|
|
|
|
}
|
|
return Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var message = $"PUT exception : {ex.Message} ({ex.GetType().Name})";
|
|
logger.LogError(message);
|
|
logger.LogError("Stack Trace : " + ex.StackTrace);
|
|
return new ObjectResult(new { ViewData, message })
|
|
{ StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|