Files
Aaru.Server/DiscImageChef.Server/Controllers/UpdateController.cs

109 lines
5.2 KiB
C#
Raw Normal View History

2019-11-02 01:40:41 +00:00
// /***************************************************************************
// 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
// ****************************************************************************/
2019-11-02 23:52:33 +00:00
using System;
2019-11-02 01:40:41 +00:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using DiscImageChef.CommonTypes.Metadata;
using DiscImageChef.Dto;
using DiscImageChef.Server.Models;
2019-11-02 21:01:25 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2019-11-02 01:40:41 +00:00
using Newtonsoft.Json;
namespace DiscImageChef.Server.Controllers
{
2019-11-02 21:01:25 +00:00
public class UpdateController : Controller
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
readonly DicServerContext _ctx;
2019-11-02 21:01:25 +00:00
2019-11-02 23:52:33 +00:00
public UpdateController(DicServerContext ctx) => _ctx = ctx;
2019-11-02 21:01:25 +00:00
2019-11-02 23:52:33 +00:00
/// <summary>Receives a report from DiscImageChef.Core, verifies it's in the correct format and stores it on the server</summary>
2019-11-02 01:40:41 +00:00
/// <returns>HTTP response</returns>
2019-11-02 23:52:33 +00:00
[Route("api/update"), HttpGet]
2019-11-02 21:01:25 +00:00
public ActionResult Update(long timestamp)
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
var sync = new SyncDto();
DateTime lastSync = DateHandlers.UnixToDateTime(timestamp);
2019-11-02 01:40:41 +00:00
sync.UsbVendors = new List<UsbVendorDto>();
2019-11-02 23:52:33 +00:00
foreach(UsbVendor vendor in _ctx.UsbVendors.Where(v => v.ModifiedWhen > lastSync))
sync.UsbVendors.Add(new UsbVendorDto
{
VendorId = vendor.VendorId, Vendor = vendor.Vendor
});
2019-11-02 01:40:41 +00:00
sync.UsbProducts = new List<UsbProductDto>();
2019-11-02 23:52:33 +00:00
foreach(UsbProduct product in _ctx.UsbProducts.Include(p => p.Vendor).Where(p => p.ModifiedWhen > lastSync))
2019-11-02 01:40:41 +00:00
sync.UsbProducts.Add(new UsbProductDto
{
2019-11-02 23:52:33 +00:00
Id = product.Id, Product = product.Product, ProductId = product.ProductId,
2019-11-02 23:34:04 +00:00
VendorId = product.Vendor.VendorId
2019-11-02 01:40:41 +00:00
});
sync.Offsets = new List<CdOffsetDto>();
2019-11-02 23:52:33 +00:00
foreach(CompactDiscOffset offset in _ctx.CdOffsets.Where(o => o.ModifiedWhen > lastSync))
2019-11-02 01:40:41 +00:00
sync.Offsets.Add(new CdOffsetDto(offset, offset.Id));
sync.Devices = new List<DeviceDto>();
2019-11-02 23:52:33 +00:00
foreach(Device device in _ctx.Devices.Where(d => d.ModifiedWhen > lastSync).ToList())
sync.Devices.Add(new DeviceDto(JsonConvert.
DeserializeObject<DeviceReportV2>(JsonConvert.SerializeObject(device,
Formatting.
None,
new
JsonSerializerSettings
{
ReferenceLoopHandling
= ReferenceLoopHandling.
Ignore
})),
device.Id, device.OptimalMultipleSectorsRead));
JsonSerializer js = JsonSerializer.Create();
var sw = new StringWriter();
2019-11-02 01:40:41 +00:00
js.Serialize(sw, sync);
2019-11-02 21:01:25 +00:00
return new ContentResult
2019-11-02 01:40:41 +00:00
{
2019-11-02 23:52:33 +00:00
StatusCode = (int)HttpStatusCode.OK, Content = sw.ToString(), ContentType = "application/json"
2019-11-02 01:40:41 +00:00
};
}
}
}