Example #1
0
        public async Task <HttpResponseMessage> Post(HttpRequestMessage req, TraceWriter log)
        {
            if (req.Content.IsMimeMultipartContent() == false)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest));
            }

            Stream fileStream     = null;
            var    streamProvider = await req.Content.ReadAsMultipartAsync(new MultipartMemoryStreamProvider());

            Driver driver         = null;
            var    filename       = string.Empty;
            string organisationId = string.Empty;

            foreach (HttpContent content in streamProvider.Contents)
            {
                Stream stream = content.ReadAsStreamAsync().Result;
                string name   = content.Headers.ContentDisposition.FileName;

                if (!string.IsNullOrEmpty(content.Headers.ContentDisposition.FileName))
                {
                    filename = content.Headers.ContentDisposition.FileName;
                    log.Info($"Filename: {content.Headers.ContentDisposition.FileName}");
                    fileStream = stream;
                }
                else
                {
                    var contents = new StreamReader(stream).ReadToEnd();
                    if (content.Headers.ContentDisposition.Name == "\"driver\"")
                    {
                        driver = JsonConvert.DeserializeObject <Driver>(contents);
                    }

                    log.Info($"{content.Headers.ContentDisposition.Name}: {contents}");
                }
            }

            if (string.IsNullOrWhiteSpace(driver.id))
            {
                driver = await _driverService.CreateAsync(driver, req);
            }

            var attachment = await _driverRepository.AddAttachment(driver.id, fileStream, "image/jpeg", filename);

            driver.PhotoId = attachment.Resource.Id;
            await _driverService.UpdateAsync(driver, req);

            return(req.CreateResponse(HttpStatusCode.OK, driver));
        }
Example #2
0
        public async Task <SavedFile> AddFileAsync(Stream fileStream, string documentId, string fileName, string name, HttpRequestMessage req)
        {
            if (string.IsNullOrEmpty(documentId))
            {
                documentId = Guid.NewGuid().ToString();
            }

            var SavedFile = new SavedFile()
            {
                name       = string.IsNullOrEmpty(name) ? fileName : name,
                fileName   = fileName,
                documentId = documentId,
            };

            SavedFile = await _SavedFileRepository.CreateItemAsync(SavedFile, req);

            var resourceResponse = await _SavedFileRepository.AddAttachment(SavedFile.id, fileStream, "application/octet-stream", SavedFile.fileName);

            SavedFile.attachmentId = resourceResponse.Resource.Id;
            SavedFile = await _SavedFileRepository.UpdateItemAsync(SavedFile.id, SavedFile, req);

            return(SavedFile);
        }