private async Task ManageUploadedFile(WorkOffer workOffer, IFormFile file) { string Id = "file-" + workOffer.Email.Replace('.', '-').Replace('@', '-'); if (!System.IO.Directory.Exists("./temp")) { System.IO.Directory.CreateDirectory("./temp"); } using (var stream = System.IO.File.Create("./temp/" + file.FileName)) { await file.CopyToAsync(stream); } var bytes = System.IO.File.ReadAllBytes("./temp/" + file.FileName); //check the existence of the asset try { var assetFound = await managementClient.GetAsset(Id); if (assetFound.SystemProperties.PublishedVersion != null) { await managementClient.UnpublishAsset(Id, assetFound.SystemProperties.Version ?? 1); } await managementClient.DeleteAsset(Id, assetFound.SystemProperties.Version ?? 1); } catch (ContentfulException) { //do nothing, the asset doesn't existst and it's ok } var managementAsset = new ManagementAsset { SystemProperties = new SystemProperties { Id = Id, Version = 1 }, Title = new Dictionary <string, string> { { "en-US", workOffer.Email } }, Files = new Dictionary <string, File> { { "en-US", new File() { ContentType = "text/plain", FileName = file.FileName } } } }; await managementClient.UploadFileAndCreateAsset(managementAsset, bytes) .ContinueWith(async delegate { // in this phase the upper function seems to make a different version is supposed to make, probably UploadFileAndCreateAsset // in order to upload and create the assets it makes 2 operation recognized by Contentful // var uploadedAsset = await managementClient.GetAsset(Id); var uploadedAsset = await managementClient.GetAsset(Id); await managementClient.PublishAsset(Id, version: uploadedAsset.SystemProperties.Version ?? 2); workOffer.Offer = await client.GetAsset(Id); }); }
/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The reader to use.</param> /// <param name="objectType">The object type to serialize into.</param> /// <param name="existingValue">The current value of the property.</param> /// <param name="serializer">The serializer to use.</param> /// <returns>The deserialized <see cref="Asset"/>.</returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(null); } var asset = new ManagementAsset(); var jObject = JObject.Load(reader); asset.Title = jObject.SelectToken("$.fields.title")?.ToObject <Dictionary <string, string> >(); asset.Description = jObject.SelectToken("$.fields.description")?.ToObject <Dictionary <string, string> >(); asset.Files = jObject.SelectToken("$.fields.file")?.ToObject <Dictionary <string, File> >(); asset.SystemProperties = jObject.SelectToken("$.sys")?.ToObject <SystemProperties>(); return(asset); }