Пример #1
0
        //async: In an asynchronous process, the application can continue with other work that doesn't depend on the web resource until the potentially blocking task finishes.
        public async Task <HttpResponseMessage> AddFile(string directory)
        {
            //kytodo fix userid
            //string userId = GetUserID();
            string      userId  = "";
            HttpContent content = Request.Content;
            string      model   = "";
            List <Tag>  tags    = new List <Tag>();

            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }


            InMemoryMultipartStreamProviderService provider = await content.ReadAsMultipartAsync(new InMemoryMultipartStreamProviderService());


            if (provider.FormData["base64Image"] == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            ItemResponse <bool> response = new ItemResponse <bool>();

            response.Item = await _fileService.AddFile(provider, directory, userId);

            return(Request.CreateResponse(response));
        }
Пример #2
0
        //InMemoryMultipartStreamProvider: custom class that extracts the data from my uploaded file
        public async Task <bool> AddFile(InMemoryMultipartStreamProviderService provider, string directory, string userId)
        {
            //instantiating an object (thing = new thing()), you set up a variable and are creating an object that your variable represents
            List <string> fileNames = new List <string>();
            List <Tag>    tags      = new List <Tag>();
            File          model     = new File();
            string        jsonTags  = provider.FormData["fileTags"];

            if (!String.IsNullOrEmpty(jsonTags))
            {
                tags = JsonConvert.DeserializeObject <List <Tag> >(jsonTags);
            }


            // = {InMemoryMultipartStreamProvider}.{form-data; name="fileUpload4"; filename="imageName.jpg"} <<<-----(files uploaded and their metadata)
            //HttpContent file = provider.Files[0];
            string fileName = await ProcessFile(provider.FormData["fileName"], directory, provider.FormData["base64Image"]);

            fileNames.Add(fileName);

            model.FileName      = fileName;
            model.FileDirectory = directory;
            model.FilePath      = "https://backslash.s3-us-west-2.amazonaws.com/" + model.FileDirectory + "/" + fileName.Replace(" ", "%20");
            model.UserId        = userId;

            using (DataContext _dc = new DataContext())
            {
                bool result = false;

                var entity = _dc.Files.FirstOrDefault(x => x.FileId == model.FileId);

                if (entity == null)
                {
                    entity               = new File();
                    entity.UserId        = model.UserId;
                    entity.FileName      = model.FileName;
                    entity.FilePath      = model.FilePath;
                    entity.FileDirectory = model.FileDirectory;
                    entity.DateAdded     = DateTimeOffset.Now;
                    entity.DateModified  = DateTimeOffset.Now;

                    _dc.Files.Add(entity);
                }
                else
                {
                    entity.FileName     = model.FileName;
                    entity.DateModified = DateTimeOffset.Now;
                }

                result = Convert.ToBoolean(_dc.SaveChanges());

                //if the upload was successful, insert or update any related tags
                if (result)
                {
                    result = AddOrUpdateFileTags(entity.FileId, tags);
                }

                return(result);
            }
        }
Пример #3
0
 public Task <bool> AddFile(InMemoryMultipartStreamProviderService provider, string directory, string userId)
 {
     return(_fileRepository.AddFile(provider, directory, userId));
 }