public async Task <UpdateResponse> UpdateCheck(UpdateRequest requestModel) { // UpdateRequest requestModel = Utilities.ReadRequest(request, this.serializer); var program = await this.work.Programs.FirstOrDefaultAsync(x => x.TelemetryKey == requestModel.TelemetryKey).ConfigureAwait(false); if (program == null) { return(new UpdateResponse() { Exception = new BadRequestException($"Program with id [{requestModel.TelemetryKey}] does not exist") }); } UpdaterPackageInfo updaterInfo = await this.work.UpdaterRepository.GetNewestCompatibleUpdater(program, requestModel.UpdaterVersion, requestModel.ToolkitVersion, false).ConfigureAwait(false); UpdateResponse response = new UpdateResponse(); if (updaterInfo != null) { var info = Mapper.Map <UpdatePackageData>(updaterInfo); info.DownloadUrl = Router.Api.DownloadUpdaterUpdate(updaterInfo); response.UpdatePackages = new[] { info }; } return(response); }
public async Task <UpdaterPackageInfo> StorePackageAsync(Updater updater, string minimumRequiredToolkitVersion, Stream fileStream, IFileSaver fileSaver) { string actualVersion = await this.streamVersionReader.GetFileVersion(fileStream, updater.FileName, true).ConfigureAwait(false); fileStream.Position = 0; fileStream = await Utilities.ZipTheStreamIfNeeded(updater.FileName, fileStream).ConfigureAwait(false); if (string.IsNullOrEmpty(minimumRequiredToolkitVersion)) { minimumRequiredToolkitVersion = "0.0.0.0"; } UpdaterPackageInfo pkg = await this.TelimenaPortalContext.UpdaterPackages.Where(x => x.FileName == updater.FileName && x.Version == actualVersion && x.MinimumRequiredToolkitVersion == minimumRequiredToolkitVersion && x.Updater.InternalName == updater.InternalName).OrderByDescending(x => x.Id).FirstOrDefaultAsync().ConfigureAwait(false); if (pkg == null) { pkg = new UpdaterPackageInfo(actualVersion, updater.FileName, fileStream.Length, minimumRequiredToolkitVersion); this.TelimenaPortalContext.UpdaterPackages.Add(pkg); pkg.Updater = updater; } pkg.UpdateContentAndMetadata(fileStream.Length); await fileSaver.SaveFile(pkg, fileStream, this.containerName, updater.PublicId).ConfigureAwait(false); return(pkg); }
public async Task <byte[]> GetPackage(int packageId, IFileRetriever fileRetriever) { UpdaterPackageInfo pkg = await this.TelimenaPortalContext.UpdaterPackages.FirstOrDefaultAsync(x => x.Id == packageId).ConfigureAwait(false); if (pkg != null) { return(await fileRetriever.GetFile(pkg, this.containerName).ConfigureAwait(false)); } return(null); }
public async Task <IHttpActionResult> Upload() { try { string reqString = HttpContext.Current.Request.Form["Model"]; UploadUpdaterRequest request = JsonConvert.DeserializeObject <UploadUpdaterRequest>(reqString); HttpPostedFile uploadedFile = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null; if (uploadedFile != null && uploadedFile.ContentLength > 0) { TelimenaUser user = await this.work.Users.GetByPrincipalAsync(this.User).ConfigureAwait(false); Updater updater = await this.work.UpdaterRepository.GetUpdater(request.UpdaterInternalName).ConfigureAwait(false); if (updater == null) { updater = this.work.UpdaterRepository.Add(uploadedFile.FileName, request.UpdaterInternalName, user); } if (user.AssociatedDeveloperAccounts.All(x => x.Id != updater.DeveloperTeam.Id)) { return(this.BadRequest( $"Updater '{updater.InternalName}' is managed by a team that you don't belong to - '{updater.DeveloperTeam.Name}'")); } if (uploadedFile.FileName != updater.FileName && !uploadedFile.FileName.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase)) { return(this.BadRequest( $"Incorrect file. Expected {updater.FileName} or a zip package with it")); } UpdaterPackageInfo pkg = await this.work.UpdaterRepository.StorePackageAsync(updater, request.MinimumCompatibleToolkitVersion, uploadedFile.InputStream, this.fileSaver).ConfigureAwait(false); await this.work.CompleteAsync().ConfigureAwait(false); return(this.Ok($"Uploaded package {pkg.Version} with ID {pkg.Id}")); } return(this.BadRequest("Empty attachment")); } catch (Exception ex) { return(this.BadRequest(ex.Message)); } }
public async Task <IHttpActionResult> Get(Guid id) { UpdaterPackageInfo updaterInfo = await this.work.UpdaterRepository.GetPackageInfo(id).ConfigureAwait(false); if (updaterInfo == null) { return(this.BadRequest($"Updater id [{id}] does not exist")); } byte[] bytes = await this.work.UpdaterRepository.GetPackage(updaterInfo.Id, this.fileRetriever).ConfigureAwait(false); HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(bytes) }; result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = updaterInfo.ZippedFileName }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip"); return(this.ResponseMessage(result)); }
public static string DownloadUpdaterUpdate(UpdaterPackageInfo pkg) { return($"{DownloadUpdaterUpdatePackage}/{pkg.PublicId}"); }