예제 #1
0
        public async Task <ActionResult> AddAFolder([FromForm] string folderPath)
        {
            //second verse, same as the first...
            //We get the path of local storage, change the path directories from / to \ if needed (epic windows style)
            //Here we check if the folder already exists in the path. This is triggered only by having the same path as
            //another folder. Much like other file systems, if the name isn't EXACTLY the same, it'll allow you to save it.
            string localStorage = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\GreenWellLocalStorage\";
            string finalPath    = localStorage + @"\" + folderPath;

            if (Directory.Exists(finalPath))
            {
                return(StatusCode(500, new { message = "The folder exists already.", status = "500" }));
            }

            try
            {
                Greenwell.Data.Models.Files folder = new Greenwell.Data.Models.Files
                {
                    FullPath = folderPath,
                    Approved = true
                };
                await _context.Files.AddAsync(folder);

                await _context.SaveChangesAsync();

                DirectoryInfo di = Directory.CreateDirectory(finalPath);

                return(Ok(new { message = "Folder was created successfully.", success = true, status = "200" }));
            }
            catch (Exception e)
            {
                return(StatusCode(500, new { error = e.Message, status = "500" }));
            }
        }
예제 #2
0
        public async Task <ActionResult> AdminAddFileFromUpload([FromForm] string path, [FromForm] IFormFile f, string[] tags, bool adminAccessOnly)
        {
            if (_context.Files.Where(f => (f.FullPath.Equals(path))).Any())
            {
                return(Ok(new { message = "Unable to upload duplicate file.", status = "201" }));
            }


            try
            {
                //If there are tags associated with the file, split at commas to create a list.
                List <int> ids = new List <int>();
                if (!string.IsNullOrEmpty(tags[0]))
                {
                    if (tags[0].Contains(","))
                    {
                        tags = tags[0].Split(",");
                    }
                    Greenwell.Data.Models.Tags ts;

                    //Add the tags to the ids list for use later.
                    for (int i = 0; i < tags.Length; i++)
                    {
                        //We check to see if the tag already exists, if its new we add it
                        var check = _context.Tags.FirstOrDefault(a => a.TagName.Equals(tags[i]));
                        if (check == null)
                        {
                            ts = new Greenwell.Data.Models.Tags
                            {
                                TagName = tags[i]
                            };
                            await _context.Tags.AddAsync(ts);

                            await _context.SaveChangesAsync();

                            ids.Add(ts.TagId);
                        }
                        //If the tag already exists, we find add that found TagId.
                        else
                        {
                            ids.Add(check.TagId);
                        }
                    }
                }
                //After collecting the tags we make a new file with the path and filename of their file,  we mark it as the corresponding admin only call and
                Greenwell.Data.Models.Files file = new Greenwell.Data.Models.Files
                {
                    FullPath  = path,
                    Filename  = System.IO.Path.GetFileName(path),
                    AdminOnly = adminAccessOnly,
                    Approved  = true
                };


                await _context.Files.AddAsync(file);

                await _context.SaveChangesAsync();

                int fileId = file.FileId;

                //Add an association between the file and the tags asynchronously.
                //Allows us to add multiple tags concurrently.
                if (ids.Count() >= 1)
                {
                    Greenwell.Data.Models.Tagmap tmps;
                    for (int i = 0; i < ids.Count; i++)
                    {
                        tmps = new Greenwell.Data.Models.Tagmap
                        {
                            TagId  = ids[i],
                            FileId = fileId
                        };
                        await _context.Tagmap.AddAsync(tmps);

                        await _context.SaveChangesAsync();
                    }
                }

                //We get the path of local storage, change the path directories from / to \ if needed (epic windows style)
                //We then use this path to save to, creating a relationship between the file and the local storage.
                string localStorage = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\GreenWellLocalStorage";
                path = path.Replace("/", @"\");
                string finalPath = @localStorage + @"\" + @path;

                using (System.IO.FileStream stream = System.IO.File.Create(finalPath))
                {
                    f.CopyTo(stream);
                    stream.Dispose();
                }
            }

            //If we receive error code 500, then something with the server went wrong. It's not specific, but
            //we can catch it and ask them to retry uploading the file.
            catch (Exception e)
            {
                return(StatusCode(500, new { error = e.Message, status = "500" }));
            }
            return(Ok(new { message = "File was added successfully.", status = "200" }));
        }