Add update endpoint.

This commit is contained in:
2019-01-01 16:43:34 +00:00
parent a95a1f71e0
commit 6fdcd6abcf
17 changed files with 628 additions and 1 deletions

View File

@@ -0,0 +1,98 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : UploadReportController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : DiscImageChef Server.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles report uploads.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2019 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using DiscImageChef.CommonTypes.Metadata;
using DiscImageChef.Dto;
using DiscImageChef.Server.Models;
using Newtonsoft.Json;
namespace DiscImageChef.Server.Controllers
{
public class UpdateController : ApiController
{
/// <summary>
/// Receives a report from DiscImageChef.Core, verifies it's in the correct format and stores it on the server
/// </summary>
/// <returns>HTTP response</returns>
[Route("api/update")]
[HttpGet]
public HttpResponseMessage UploadReport(long timestamp)
{
DicServerContext ctx = new DicServerContext();
SyncDto sync = new SyncDto();
DateTime lastSync = DateHandlers.UnixToDateTime(timestamp);
sync.UsbVendors = new List<UsbVendorDto>();
foreach(UsbVendor vendor in ctx.UsbVendors.Where(v => v.ModifiedWhen > lastSync))
sync.UsbVendors.Add(new UsbVendorDto {VendorId = (ushort)vendor.VendorId, Vendor = vendor.Vendor});
sync.UsbProducts = new List<UsbProductDto>();
foreach(UsbProduct product in ctx.UsbProducts.Where(p => p.ModifiedWhen > lastSync))
sync.UsbProducts.Add(new UsbProductDto
{
Id = product.Id,
Product = product.Product,
ProductId = (ushort)product.ProductId,
VendorId = (ushort)product.VendorId
});
sync.Offsets = new List<CdOffsetDto>();
foreach(CompactDiscOffset offset in ctx.CdOffsets.Where(o => o.ModifiedWhen > lastSync))
sync.Offsets.Add(new CdOffsetDto(offset, offset.Id));
sync.Devices = new List<DeviceDto>();
foreach(Device device in ctx.Devices.Where(d => d.ModifiedWhen > lastSync).ToList())
sync.Devices.Add(new
DeviceDto(JsonConvert.DeserializeObject<DeviceReportV2>(JsonConvert.SerializeObject(device)),
device.Id));
JsonSerializer js = JsonSerializer.Create();
StringWriter sw = new StringWriter();
js.Serialize(sw, sync);
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(sw.ToString(), Encoding.UTF8, "application/json")
};
}
}
}

View File

@@ -210,6 +210,7 @@
<Compile Include="Controllers\ReportController.cs" />
<Compile Include="Controllers\StatsController.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\UpdateController.cs" />
<Compile Include="Controllers\UploadReportController.cs" />
<Compile Include="Controllers\UploadStatsController.cs" />
<Compile Include="Global.asax.cs">
@@ -336,6 +337,10 @@
<Project>{0beb3088-b634-4289-ae17-cdf2d25d00d5}</Project>
<Name>DiscImageChef.Decoders</Name>
</ProjectReference>
<ProjectReference Include="..\DiscImageChef.Dto\DiscImageChef.Dto.csproj">
<Project>{f4399ff5-9bd0-475a-9ea7-3dae45291fe2}</Project>
<Name>DiscImageChef.Dto</Name>
</ProjectReference>
<ProjectReference Include="..\DiscImageChef.Helpers\DiscImageChef.Helpers.csproj">
<Project>{f8bdf57b-1571-4cd0-84b3-b422088d359a}</Project>
<Name>DiscImageChef.Helpers</Name>
@@ -376,6 +381,9 @@
<DependentUpon>201812252219066_StoreReadResultsInReportDatabase.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Folder Include="DTOs" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@@ -51,6 +51,16 @@ namespace DiscImageChef.Server.Models
AddedWhen = ModifiedWhen = DateTime.UtcNow;
}
public CompactDiscOffset(CdOffset offset)
{
Manufacturer = offset.Manufacturer;
Model = offset.Model;
Offset = offset.Offset;
Submissions = offset.Submissions;
Agreement = offset.Agreement;
AddedWhen = ModifiedWhen = DateTime.UtcNow;
}
public int Id { get; set; }
public DateTime AddedWhen { get; set; }
[Index]

View File

@@ -33,6 +33,7 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Newtonsoft.Json;
namespace DiscImageChef.Server.Models
{
@@ -57,7 +58,8 @@ namespace DiscImageChef.Server.Models
[Index]
public DateTime ModifiedWhen { get; set; }
[Index]
public int VendorId { get; set; }
public int VendorId { get; set; }
[JsonIgnore]
public virtual UsbVendor Vendor { get; set; }
}
}

View File

@@ -34,6 +34,7 @@ using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Newtonsoft.Json;
namespace DiscImageChef.Server.Models
{
@@ -57,6 +58,7 @@ namespace DiscImageChef.Server.Models
[Index]
public DateTime ModifiedWhen { get; set; }
[JsonIgnore]
public virtual ICollection<UsbProduct> Products { get; set; }
}
}