Exemplo n.º 1
0
        public IHttpActionResult GetOutFiles(string token)
        {
            Token _token = db.Tokens.Include(t => t.User).FirstOrDefault(t => t._Token == token);

            if (_token == null)
            {
                return(Content(HttpStatusCode.Unauthorized, "Token Does Not Exist"));
            }
            if (_token.DateExpiered.CompareTo(DateTime.Now) < 0)
            {
                return(Content(HttpStatusCode.Unauthorized, "Token Expired"));
            }
            string        json     = System.IO.File.ReadAllText($@"{FilesController.PathLocation}\ApplicationSettings.json");
            OutSettings   Settings = JsonConvert.DeserializeObject <OutSettings>(json);
            List <string> s        = new List <string>();

            foreach (var item in Settings.FileOut)
            {
                s.Add(item.Key + $"[{item.Value}]");
            }
            return(Ok(s));
        }
Exemplo n.º 2
0
        public IHttpActionResult DownloadFile(string token, string filename)
        {
            Token _token = db.Tokens.Include(t => t.User).FirstOrDefault(t => t._Token == token);

            if (_token == null)
            {
                return(Content(HttpStatusCode.Unauthorized, "Token Does Not Exist"));
            }
            if (_token.DateExpiered.CompareTo(DateTime.Now) < 0)
            {
                return(Content(HttpStatusCode.Unauthorized, "Token Expired"));
            }

            File _file = db.Files.Where(f => f.Path.Contains(filename) && f.WLevel >= _token.User.Level).FirstOrDefault();

            if (_file == null)
            {
                return(NotFound());
            }
            OutSettings o;

            try
            {
                string json1 = System.IO.File.ReadAllText($@"{PathLocation}\ApplicationSettings.json");
                o = JsonConvert.DeserializeObject <OutSettings>(json1);
            }
            catch (Exception)
            {
                o = new OutSettings();
            }
            o.FileOut.Add(_file.PathParts.Last(), _token.User.ID);
            string json = JsonConvert.SerializeObject(o);

            System.IO.File.WriteAllText($@"{PathLocation}\ApplicationSettings.json", json);
            byte[] temp = System.IO.File.ReadAllBytes(_file.Path);
            return(Ok(Convert.ToBase64String(temp)));
        }
Exemplo n.º 3
0
        public IHttpActionResult UploadFile(string token, TFile file, string returnfile = "false")
        {
            Token _token = db.Tokens.Include(t => t.User).FirstOrDefault(t => t._Token == token);

            if (_token == null)
            {
                return(Content(HttpStatusCode.Unauthorized, "Token Does Not Exist"));
            }
            if (_token.DateExpiered.CompareTo(DateTime.Now) < 0)
            {
                return(Content(HttpStatusCode.Unauthorized, "Token Expired"));
            }

            if (returnfile == "true")
            {
                if (db.Files.Where(f => f.Path == $@"{PathLocation}\{file.Path}" && f.WLevel >= _token.User.Level).FirstOrDefault() == null)
                {
                    return(Content(HttpStatusCode.Unauthorized, "File Does Not Exist"));
                }
            }
            else
            {
                int coppy = 1;
                while (System.IO.File.Exists($@"{PathLocation}\{file.Path}"))
                {
                    try
                    {
                        file.Path = file.Path.Substring(0, file.Path.Length - file.Path.Split('_').Last().Length - 1) + $"_{coppy}." + file.Path.Split('.').Last();
                    }
                    catch
                    {
                        file.Path = file.Path.Substring(0, file.Path.Length - file.Path.Split('.').Last().Length - 1) + $"_{coppy}." + file.Path.Split('.').Last();
                    }
                    coppy++;
                }
            }

            System.IO.File.WriteAllBytes($@"{PathLocation}\{file.Path}", file.File);

            Damato_API.DataBase.File file2 = new Damato_API.DataBase.File()
            {
                Path  = $@"{PathLocation}\{file.Path}",
                Level = $"{_token.User.Level},{_token.User.Level},{_token.User.Level}"
            };

            if (returnfile == "true")
            {
                string      json = System.IO.File.ReadAllText($@"{PathLocation}\ApplicationSettings.json");
                OutSettings o    = JsonConvert.DeserializeObject <OutSettings>(json);
                o.FileOut.Remove(file2.PathParts.Last());
                json = JsonConvert.SerializeObject(o);
                System.IO.File.WriteAllText($@"{PathLocation}\ApplicationSettings.json", json);
            }
            else
            {
                db.Files.Add(file2);
                db.SaveChanges();
                db.Entry(file2).Reload();
                var user = db.Users.ToList().FirstOrDefault(u => u.ID == _token.User.ID);
                file2.User = user;
                db.SaveChanges();
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }