/// <summary> /// Deletes the attachment from the API. /// </summary> /// <param name="api"></param> /// <returns></returns> public async Task DeleteAsync(ITimelineService api) { await api.PutJsonAsync("TimelineEventAttachment/Delete", new { AttachmentId = Id }); // Delete attachment from disk if it's there. var file = Path.Combine(api.CacheFolder, Name); if (api.FileExists(file)) { api.FileDelete(file); } }
/// <summary> /// Downloads the attachment file, or gets it from the cache if it's already been downloaded. /// </summary> public async Task <string> DownloadOrCacheAsync(ITimelineService api) { string url = await GenerateGetPresignedUrlAsync(api); // Download attachment if it doesn't exist in the cache. var file = Path.Combine(api.CacheFolder, Name); if (!api.FileExists(file)) { await api.DownloadFileAsync(url, file); } Debug.WriteLine("URL: " + url); Debug.WriteLine("Filename: " + file); return(file); }
/// <summary> /// Creates a new attachment and uploads the file in a single action, attaching it to the specified event. /// </summary> /// <param name="api">The API to create the attachment on.</param> /// <param name="eventId">The ID of the event the attachment is being attached to.</param> /// <param name="filename">The name of the attachment file on the local web server.</param> /// <param name="fileStream">A stream object containing the file contents.</param> /// <returns>The new attachment</returns> public static async Task <Attachment> CreateAndUploadAsync(ITimelineService api, string eventId, string filename, Stream fileStream) { var attachment = await CreateAsync(api, eventId, filename); string temp = Path.GetTempFileName(); try { await CopyFileFromStream(api, fileStream, temp); await attachment.UploadAsync(api, temp); } finally { if (api.FileExists(temp)) { api.FileDelete(temp); } } return(attachment); }