public async Task <IActionResult> GetMetadata(long id, [FromHeader(Name = HeaderNames.IfNoneMatch)] string ifNoneMatch) { var latestVersion = await _objectsStorageReader.GetObjectLatestVersion(id); if (latestVersion == null) { return(NotFound()); } Response.Headers[HeaderNames.ETag] = $"\"{latestVersion.VersionId}\""; Response.Headers[HeaderNames.LastModified] = latestVersion.LastModified.ToString("R"); if (!string.IsNullOrEmpty(ifNoneMatch) && ifNoneMatch.Trim('"') == latestVersion.VersionId) { return(NotModified()); } return(Ok()); }
private async Task <string> PutObject( long id, string versionId, AuthorInfo authorInfo, IEnumerable <IObjectElementDescriptor> currentObjectElements, IObjectDescriptor objectDescriptor) { PreprocessObjectElements(objectDescriptor.Elements); await VerifyObjectElementsConsistency(id, objectDescriptor.Language, objectDescriptor.Elements); var metadataForBinaries = await RetrieveMetadataForBinaries(id, currentObjectElements, objectDescriptor.Elements); await _eventSender.SendAsync(_objectEventsTopic, new ObjectVersionCreatingEvent(id, versionId)); var totalBinariesCount = 0; PutObjectRequest putRequest; MetadataCollectionWrapper metadataWrapper; foreach (var elementDescriptor in objectDescriptor.Elements) { var(elementPersistenceValue, binariesCount) = ConvertToPersistenceValue(elementDescriptor.Value, metadataForBinaries); var elementPersistenceDescriptor = new ObjectElementPersistenceDescriptor(elementDescriptor, elementPersistenceValue); totalBinariesCount += binariesCount; putRequest = new PutObjectRequest { Key = id.AsS3ObjectKey(elementDescriptor.Id), BucketName = _bucketName, ContentType = ContentType.Json, ContentBody = JsonConvert.SerializeObject(elementPersistenceDescriptor, SerializerSettings.Default), CannedACL = S3CannedACL.PublicRead }; metadataWrapper = MetadataCollectionWrapper.For(putRequest.Metadata); metadataWrapper.Write(MetadataElement.Author, authorInfo.Author); metadataWrapper.Write(MetadataElement.AuthorLogin, authorInfo.AuthorLogin); metadataWrapper.Write(MetadataElement.AuthorName, authorInfo.AuthorName); await _s3Client.PutObjectAsync(putRequest); } var objectKey = id.AsS3ObjectKey(Tokens.ObjectPostfix); var elementVersions = await _objectsStorageReader.GetObjectElementsLatestVersions(id); var objectPersistenceDescriptor = new ObjectPersistenceDescriptor { TemplateId = objectDescriptor.TemplateId, TemplateVersionId = objectDescriptor.TemplateVersionId, Language = objectDescriptor.Language, Properties = objectDescriptor.Properties, Elements = elementVersions }; putRequest = new PutObjectRequest { Key = objectKey, BucketName = _bucketName, ContentType = ContentType.Json, ContentBody = JsonConvert.SerializeObject(objectPersistenceDescriptor, SerializerSettings.Default), CannedACL = S3CannedACL.PublicRead }; metadataWrapper = MetadataCollectionWrapper.For(putRequest.Metadata); metadataWrapper.Write(MetadataElement.Author, authorInfo.Author); metadataWrapper.Write(MetadataElement.AuthorLogin, authorInfo.AuthorLogin); metadataWrapper.Write(MetadataElement.AuthorName, authorInfo.AuthorName); metadataWrapper.Write( MetadataElement.ModifiedElements, string.Join(Tokens.ModifiedElementsDelimiter.ToString(), objectDescriptor.Elements.Select(x => x.TemplateCode))); await _s3Client.PutObjectAsync(putRequest); _referencedBinariesMetric.Inc(totalBinariesCount); var objectLatestVersion = await _objectsStorageReader.GetObjectLatestVersion(id); return(objectLatestVersion.VersionId); }