Пример #1
0
        public async Task <JsonResult> CreateContent([FromBody] InfoLiecViewModel vm)
        {
            var imagePath = Path.Combine(_hostingEnvironment.WebRootPath, "static/Images");
            var content   = InfoLiecViewmodelToContentModel(vm);
            var dir       = new DirectoryInfo(imagePath);

            // Tags and Sources to lower
            for (int i = 0; i < content.Tags.Length; i++)
            {
                content.Tags[i] = content.Tags[i].ToLower();
            }

            if (!dir.Exists)
            {
                dir.Create();
            }

            // Copy and delete temp image file
            var      cleanPath    = content.ImagePath.Remove(0, 1);
            var      tempFilePath = Path.Combine(_hostingEnvironment.WebRootPath, cleanPath);
            FileInfo imageFile    = new FileInfo(tempFilePath);
            var      newFilePath  = Path.Combine(dir.FullName, imageFile.Name);

            imageFile.CopyTo(newFilePath);
            content.ImagePath = newFilePath.Remove(0, _hostingEnvironment.WebRootPath.Length);
            imageFile.Delete();

            try
            {
                await MongoDbContext.Content_Create(content);
            }
            catch (Exception e)
            {
                return(Json(BadRequest(e)));
            }
            return(Json(Ok()));
        }