Exemplo n.º 1
0
        //[DisableFormValueModelBinding]
        //[RequestFormLimits(MultipartBodyLengthLimit = 200 * 1024 * 1024)]
        public IActionResult UpLoad()
        {
            string token = Request.Cookies["Token"];

            string[] p = Request.Headers["Path"].ToString().Split('/');
            //string[] p = "./A/".Split('/');
            var files = Request.Form.Files;

            if (Token.CheckToken(token) && files != null)
            {
                Stream stream       = null;
                string saveFilePath = null;
                JArray filestr      = null;
                string name         = null;
                try
                {
                    foreach (var file in files)
                    {
                        string ID = token.Split("-")[0];
                        ID = ID.Substring(0, ID.Length - 10);
                        DataTable table;
                        if ((table = SQLControl.Select($"SELECT * FROM testbase.UserTable where  ID={ID};")) == null)
                        {
                            throw new NewSqlException();
                        }
                        name = table.Rows[0][1].ToString();

                        filestr = null;
                        stream  = file.OpenReadStream();
                        byte[] hash = Save_ReadFile.GetHash(stream);

                        MySqlParameter hashParameter = new MySqlParameter("@hash", MySqlDbType.TinyBlob);
                        hashParameter.Value = hash;
                        int fileID = 0;
                        //检查Hash表里是否有这个文件
                        if (-1 == (fileID = SQLControl.Select($"SELECT * FROM testbase.HashTable where Hash = @hash;", hashParameter)))
                        {//没有就加进去
                            string filename = DateTime.Now.ToString("yyyyMMddhhmmss");
                            while (System.IO.File.Exists(FilePath + filename))
                            {
                                filename = DateTime.Now.ToString("yyyyMMddhhmmss") + new Random().Next(10);
                            }
                            if (!Save_ReadFile.SaveFile(FilePath + filename, file))
                            {
                                throw new IOException();
                            }
                            MySqlParameter blobParameter = new MySqlParameter("@hash", MySqlDbType.TinyBlob);
                            blobParameter.Value = hash;
                            if (0 == SQLControl.Execute($"insert testbase.HashTable value (0,@hash,'{FilePath + filename}');", blobParameter))
                            {
                                throw new NewSqlException();
                            }
                            fileID = SQLControl.Select($"SELECT * FROM testbase.HashTable where Hash = @hash;", blobParameter);
                        }
                        //把Hash绑定到用户文件列表上
                        do
                        {
                            table = null;
                            if ((table = SQLControl.Select($"SELECT * FROM testbase.UserFileTable where UserName='******';")) == null)
                            {
                                throw new NewSqlException();
                            }
                        } while (int.Parse(table.Rows[0][2].ToString()) != 1);
                        filestr = JArray.Parse(table.Rows[0][1].ToString());
                        Queue <string> pathlist = new Queue <string>(p);
                        JObject        jo       = new JObject();
                        Encoding       encoding = Encoding.Default;
                        jo.Add("time", DateTime.Now.ToString("yyyy-MM-dd"));
                        jo.Add("name", file.FileName);
                        jo.Add("type", "file");
                        jo.Add("size", file.Length);
                        jo.Add("data", fileID);
                        JToken newdir = Dir.AddJson(filestr, pathlist, JToken.Parse(jo.ToString()));

                        SQLControl.Execute($"UPDATE testbase.UserFileTable SET File='' , State = 0 where UserName='******';");
                        SQLControl.Execute($"UPDATE testbase.UserFileTable SET File='{newdir.ToString()}',State = 1 where UserName='******';");
                    }
                }
                catch (IOException)
                {
                    if (stream != null)
                    {
                        stream.Close();
                    }
                    if (System.IO.File.Exists(saveFilePath))
                    {
                        System.IO.File.Delete(saveFilePath);
                    }
                    return(StatusCode(500, JsonConvert.SerializeObject(new ReturnMode()
                    {
                        Data = "服务器保存错误", Message = "Error"
                    })));
                }
                catch (NewSqlException)
                {
                    return(StatusCode(500, JsonConvert.SerializeObject(new ReturnMode()
                    {
                        Data = "数据库错误", Message = "Error"
                    })));
                }
                catch (MySqlException)
                {
                    if (filestr != null)
                    {
                        SQLControl.Execute($"UPDATE testbase.UserFileTable SET File='{filestr.ToString()}' , State = 1 where UserName='******';");
                    }
                }
            }
            else
            {
                return(BadRequest(JsonConvert.SerializeObject(new ReturnMode()
                {
                    Data = "Token错误", Message = "Error"
                })));
            }
            return(Ok(JsonConvert.SerializeObject(new ReturnMode()
            {
                Data = "保存完成!", Message = "OK"
            })));
        }
Exemplo n.º 2
0
        public IActionResult AddDir()
        {
            string token = Request.Cookies["Token"];

            string[]       p       = Request.Headers["Path"].ToString().Split('/');
            string         dirname = Request.Headers["DirName"].ToString();
            string         name    = "";
            JArray         filestr = null;
            Queue <string> q       = new Queue <string>(p);
            DataTable      table   = null;

            try
            {
                if (Token.CheckToken(token))
                {
                    string ID = token.Split("-")[0];
                    ID = ID.Substring(0, ID.Length - 10);
                    if ((table = SQLControl.Select($"SELECT * FROM testbase.UserTable where  ID={ID};")) == null)
                    {
                        throw new NewSqlException();
                    }
                    name = table.Rows[0][1].ToString();
                    if ((table = SQLControl.Select($"SELECT * FROM testbase.UserFileTable where UserName='******';")) == null)
                    {
                        throw new NewSqlException();
                    }
                    do
                    {
                        table = null;
                        if ((table = SQLControl.Select($"SELECT * FROM testbase.UserFileTable where UserName='******';")) == null)
                        {
                            throw new NewSqlException();
                        }
                    } while (int.Parse(table.Rows[0][2].ToString()) != 1);
                    filestr = JArray.Parse(table.Rows[0][1].ToString());
                    JObject job = new JObject();
                    job["time"] = DateTime.Now.ToString("yyyy-MM-dd");
                    job["name"] = dirname;
                    job["type"] = "dir";
                    job["data"] = "[]";
                    JToken newdir = Dir.AddJson(filestr, q, JToken.Parse(job.ToString()));

                    SQLControl.Execute($"UPDATE testbase.UserFileTable SET File='' , State = 0 where UserName='******';");
                    SQLControl.Execute($"UPDATE testbase.UserFileTable SET File='{newdir.ToString()}',State = 1 where UserName='******';");
                }
                else
                {
                    return(BadRequest(JsonConvert.SerializeObject(new ReturnMode()
                    {
                        Data = "Token错误", Message = "Error"
                    })));
                }
            }
            catch (NewSqlException)
            {
                return(StatusCode(500, JsonConvert.SerializeObject(new ReturnMode()
                {
                    Data = "数据库错误", Message = "Error"
                })));
            }
            catch (MySqlException)
            {
                if (filestr != null)
                {
                    SQLControl.Execute($"UPDATE testbase.UserFileTable SET File='{filestr.ToString()}' , State = 1 where UserName='******';");
                }
            }
            return(Ok());
        }