using Microsoft.Extensions.Options; using System.Net; using System.Net.Http; using DigitalData.Core.Legacy.Client; using System.Threading.Tasks; using WindreamHub.Legacy.Client.Models; using WindreamHub.Legacy.Client.Models.Documents.Response; using WindreamHub.Legacy.Client.Models.Documents.Create.Request; using System.IO; using System.Web; namespace WindreamHub.Legacy.Client.Routes { public class DocumentsRouteService : BaseRouteService { public DocumentsRouteService(HttpClient client, CookieContainer cookieContainer, IOptions clientOptions) : base(client, cookieContainer, clientOptions) { } public async Task> Create(HttpContent docCreateBody) => await FetchAsync(route: "/Create", method: HttpMethod.Post, body: docCreateBody) .ThenAsync(res => res.Simplify()); public async Task> Create(DocCreateBody docCreateBody) => await FetchAsync(route: "/Create", method: HttpMethod.Post, body: docCreateBody.Stringify()) .ThenAsync(res => res.Simplify()); public async Task> Upload(string path, long? item_id = null, string item_location = null, string item_name = null, object stream_identity = null, params int[] flags) { using (var fileStream = File.OpenRead(path)) { var fileContent = new StreamContent(fileStream); var formData = new MultipartFormDataContent { { fileContent, "file", "filename.pdf" } }; var query = HttpUtility.ParseQueryString(string.Empty); if (item_id != null) query["parameter.item.id"] = item_id.ToString(); if (item_location != null) query["parameter.item.location"] = item_location; if (item_name != null) query["parameter.item.name"] = item_name; if (flags != null && flags.Length > 0) query["parameter.flags"] = string.Join(",", flags); if (stream_identity != null) query["parameter.stream.__identity"] = stream_identity.ToString(); return await FetchAsync(route: "/Upload", method: HttpMethod.Post, body: fileContent) .ThenAsync(res => res.Simplify()); } } } }