コード例 #1
0
        public async Task <IActionResult> UpdateDevice(string address)
        {
            var addressId = await GetAddressId(address);

            Database.Entities.Device device = await _repo.GetDeviceByAddresId(addressId);

            if (device == null)
            {
                return(StatusCode(304));
            }

            if (device.IsAutoUpdate.HasValue && device.IsAutoUpdate.Value == true)
            {
                if (!device.KindId.HasValue || !device.ComponentId.HasValue)
                {
                    return(StatusCode(304));
                }

                var versionId = await _repo.GetLatestVersionId(device.KindId, device.ComponentId) ?? 0;

                if (versionId > 0 && versionId != device.VersionId)
                {
                    device.VersionId = versionId;
                    device.IsUpdated = false;
                    device.Updated   = null;

                    await _repo.SaveAllChangesAsync();
                }
            }

            if (device.IsUpdated.HasValue && device.IsUpdated.Value == true)
            {
                return(StatusCode(304));
            }

            Database.Entities.Version version = null;

            if (device.VersionId.HasValue)
            {
                version = await _repo.GetVersionById(device.VersionId.Value);
            }

            if (version == null)
            {
                return(StatusCode(304));
            }

            var file = version.FileData;

            var content     = new MemoryStream(file.Content);
            var contentType = "APPLICATION/octet-stream";
            var fileName    = $"{file.Name}{file.Extension}";

            return(File(content, contentType, fileName));
        }