Exemplo n.º 1
0
        private static long RunTestBlockWriter(string path, int bufferSize)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            long ioCount = 0;

            using (var blockWriter = new FileBlockWriter(path, bufferSize))
            {
                var buffer = new byte[bufferSize];
                var start  = DateTime.UtcNow;

                while (DateTime.UtcNow - start < TimeSpan.FromSeconds(TestDuration)) // run the test for 10sec
                {
                    blockWriter.Write(buffer, true);
                    ioCount++;
                }

                var durationInMs = (DateTime.UtcNow - start).TotalMilliseconds;
                PrintReport("FileBlockWriter", bufferSize, ioCount, durationInMs);
            }

            return(ioCount);
        }
Exemplo n.º 2
0
        void file_create()
        {
            long            gb = 1073741824;
            FileBlockWriter fw = new FileBlockWriter();

            fw.make("d:\\test.db", gb * 10);
        }
Exemplo n.º 3
0
        private static long RunTestJournalWriter(string path, int bufferSize)
        {
            if(File.Exists(path))
            {
                File.Delete(path);
            }

            long ioCount = 0;
            var blockWriter = new FileBlockWriter(path, bufferSize);
            using (var journalWriter = new JournalWriter(bufferSize, blockWriter))
            {
                var buffer = new ArraySegment<byte>(new byte[bufferSize]);
                var start = DateTime.UtcNow;

                while (DateTime.UtcNow - start < TimeSpan.FromSeconds(TestDuration)) // run the test for 10sec
                {
                    journalWriter.WriteBytes(buffer, true);
                    ioCount++;
                }

                var durationInMs = (DateTime.UtcNow - start).TotalMilliseconds;
                PrintReport("JournalWriter", bufferSize, ioCount, durationInMs);
            }
            return ioCount;
        }
Exemplo n.º 4
0
        void savePart()
        {
            HttpPostedFile part = Request.Files.Get(0);

            //验证大小
            if (part.InputStream.Length != long.Parse(this.blockSize))
            {
                Response.End();
                return;
            }
            //计算块md5
            string md5Svr = string.Empty;

            if (!string.IsNullOrEmpty(this.blockMd5))
            {
                md5Svr = this.mkMD5(part.InputStream);
            }

            FileBlockWriter fbw = new FileBlockWriter();

            fbw.write(pathSvr, long.Parse(this.lenLoc), long.Parse(this.blockOffset), ref part);

            //触发事件
            up7_biz_event.file_post_block(id, int.Parse(blockIndex));

            //返回信息
            JObject o = new JObject();

            o["msg"]    = "ok";
            o["md5"]    = md5Svr;
            o["offset"] = long.Parse(this.blockOffset);
            string json = JsonConvert.SerializeObject(o);//取消格式化

            Response.Write(json);
        }
Exemplo n.º 5
0
        void savePartFolder()
        {
            FileInf fileSvr = new FileInf();

            fileSvr.id         = id;
            fileSvr.nameLoc    = Path.GetFileName(pathLoc);
            fileSvr.nameSvr    = nameLoc;
            fileSvr.lenLoc     = long.Parse(lenLoc);
            fileSvr.sizeLoc    = sizeLoc;
            fileSvr.pathLoc    = this.pathLoc;
            fileSvr.pathSvr    = this.pathSvr;
            fileSvr.pathRel    = this.pathRel;
            fileSvr.pid        = this.pid;
            fileSvr.pidRoot    = pidRoot;
            fileSvr.blockCount = int.Parse(blockCount);
            fileSvr.blockSize  = int.Parse(blockSize);

            HttpPostedFile part = Request.Files.Get(0);

            //验证大小
            if (part.InputStream.Length != long.Parse(this.blockSize))
            {
                Response.End();
                return;
            }

            //计算块md5
            string md5Svr = string.Empty;

            if (!string.IsNullOrEmpty(this.blockMd5))
            {
                md5Svr = this.mkMD5(part.InputStream);
            }

            //保存块数据
            FileBlockWriter fbw = new FileBlockWriter();

            fbw.write(fileSvr.pathSvr, fileSvr.lenLoc, long.Parse(this.blockOffset), ref part);

            //触发事件
            up7_biz_event.file_post_block(id, int.Parse(blockIndex));

            //返回信息
            JObject o = new JObject();

            o["msg"]    = "ok";
            o["md5"]    = md5Svr;
            o["offset"] = long.Parse(this.blockOffset);
            string json = JsonConvert.SerializeObject(o);//取消格式化

            Response.Write(json);
        }
Exemplo n.º 6
0
        void file_init()
        {
            string md5      = Request.QueryString["md5"];
            string id       = Request.QueryString["id"];
            string pid      = Request.QueryString["pid"];
            string pidRoot  = Request.QueryString["pidRoot"];
            string uid      = Request.QueryString["uid"];
            string lenLoc   = Request.QueryString["lenLoc"];
            string sizeLoc  = Request.QueryString["sizeLoc"];
            string callback = Request.QueryString["callback"];                      //jsonp参数
            //客户端使用的是encodeURIComponent编码,
            string pathLoc = HttpUtility.UrlDecode(Request.QueryString["pathLoc"]); //utf-8解码

            //参数为空
            if (string.IsNullOrEmpty(md5) ||
                string.IsNullOrEmpty(uid) ||
                string.IsNullOrEmpty(sizeLoc))
            {
                Response.Write(callback + "({\"value\":null})");
                return;
            }

            FileInf fileSvr = new FileInf();

            fileSvr.fdChild = false;
            fileSvr.uid     = int.Parse(uid);//将当前文件UID设置为当前用户UID
            fileSvr.id      = id;
            fileSvr.pid     = pid;
            fileSvr.pidRoot = pidRoot;
            fileSvr.nameLoc = Path.GetFileName(pathLoc);
            fileSvr.pathLoc = pathLoc;
            fileSvr.lenLoc  = Convert.ToInt64(lenLoc);
            fileSvr.sizeLoc = sizeLoc;
            fileSvr.deleted = false;
            fileSvr.md5     = md5;
            fileSvr.nameSvr = fileSvr.nameLoc;

            //所有单个文件均以uuid/file方式存储
            PathBuilderUuid pb = new PathBuilderUuid();

            fileSvr.pathSvr = pb.genFile(fileSvr.uid, ref fileSvr);
            fileSvr.pathSvr = fileSvr.pathSvr.Replace("\\", "/");

            //数据库存在相同文件
            DBFile  db        = new DBFile();
            FileInf fileExist = new FileInf();

            if (db.exist_file(md5, ref fileExist))
            {
                fileSvr.nameSvr  = fileExist.nameSvr;
                fileSvr.pathSvr  = fileExist.pathSvr;
                fileSvr.perSvr   = fileExist.perSvr;
                fileSvr.lenSvr   = fileExist.lenSvr;
                fileSvr.complete = fileExist.complete;
                db.Add(ref fileSvr);

                //触发事件
                up6_biz_event.file_create_same(fileSvr);
            }//数据库不存在相同文件
            else
            {
                db.Add(ref fileSvr);
                //触发事件
                up6_biz_event.file_create(fileSvr);

                //2.0创建器。仅创建一个空白文件
                FileBlockWriter fr = new FileBlockWriter();
                fr.make(fileSvr.pathSvr, fileSvr.lenLoc);
            }
            string jv = JsonConvert.SerializeObject(fileSvr);

            jv = HttpUtility.UrlEncode(jv);
            jv = jv.Replace("+", "%20");
            string json = callback + "({\"value\":\"" + jv + "\"})";//返回jsonp格式数据。

            Response.Write(json);
        }
Exemplo n.º 7
0
        void file_post()
        {
            string uid         = Request.Headers["uid"];
            string f_id        = Request.Headers["id"];
            string lenSvr      = Request.Headers["lenSvr"];     //已传大小
            string lenLoc      = Request.Headers["lenLoc"];     //本地文件大小
            string blockOffset = Request.Headers["blockOffset"];
            string blockSize   = Request.Headers["blockSize"];  //当前块大小
            string blockIndex  = Request.Headers["blockIndex"]; //当前块索引,基于1
            string blockMd5    = Request.Headers["blockMd5"];   //块MD5
            string complete    = Request.Headers["complete"];   //true/false
            string pathSvr     = Request.Form["pathSvr"];       //add(2015-03-19):

            pathSvr = HttpUtility.UrlDecode(pathSvr);

            if (this.head_val_null_empty("lenLoc, uid, id, blockOffset"))
            {
                return;
            }

            if (Request.Files.Count < 1)
            {
                PageTool.to_content("file is empty");

                return;
            }

            bool           verify = false;
            string         msg    = string.Empty;
            string         md5Svr = string.Empty;
            HttpPostedFile file   = Request.Files.Get(0);//文件块

            //计算文件块MD5
            if (!string.IsNullOrEmpty(blockMd5))
            {
                md5Svr = Md5Tool.calc(file.InputStream);
            }

            //文件块大小验证
            verify = int.Parse(blockSize) == file.InputStream.Length;
            if (!verify)
            {
                msg = "block size error sizeSvr:" + file.InputStream.Length + " sizeLoc:" + blockSize;
            }

            //块MD5验证
            if (verify && !string.IsNullOrEmpty(blockMd5))
            {
                verify = md5Svr == blockMd5;
                if (!verify)
                {
                    msg = "block md5 error";
                }
            }

            if (verify)
            {
                //2.0保存文件块数据
                FileBlockWriter res = new FileBlockWriter();
                res.make(pathSvr, Convert.ToInt64(lenLoc));
                res.write(pathSvr, Convert.ToInt64(blockOffset), ref file);
                up6_biz_event.file_post_block(f_id, Convert.ToInt32(blockIndex));

                //生成信息
                JObject o = new JObject();
                o["msg"]    = "ok";
                o["md5"]    = md5Svr;      //文件块MD5
                o["offset"] = blockOffset; //偏移
                msg         = JsonConvert.SerializeObject(o);
            }
            PageTool.to_content(msg);
        }
Exemplo n.º 8
0
        void f_create()
        {
            string pid      = Request.QueryString["pid"];
            string pidRoot  = Request.QueryString["pidRoot"];
            string md5      = Request.QueryString["md5"];
            string id       = Request.QueryString["id"];
            string uid      = Request.QueryString["uid"];
            string lenLoc   = Request.QueryString["lenLoc"];
            string sizeLoc  = Request.QueryString["sizeLoc"];
            string callback = Request.QueryString["callback"];                      //jsonp参数
            //客户端使用的是encodeURIComponent编码,
            string pathLoc = HttpUtility.UrlDecode(Request.QueryString["pathLoc"]); //utf-8解码
            string pathRel = this.reqString("pathRel");

            if (string.IsNullOrEmpty(pid))
            {
                pid = string.Empty;
            }
            if (string.IsNullOrEmpty(pidRoot))
            {
                pidRoot = pid;
            }

            //参数为空
            if (string.IsNullOrEmpty(md5) ||
                string.IsNullOrEmpty(uid) ||
                string.IsNullOrEmpty(sizeLoc))
            {
                Response.Write(callback + "({\"value\":null})");
                return;
            }

            FileInf fileSvr = new FileInf();

            fileSvr.fdChild = false;
            fileSvr.uid     = int.Parse(uid);//将当前文件UID设置为当前用户UID
            fileSvr.id      = id;
            fileSvr.pid     = pid;
            fileSvr.fdChild = !string.IsNullOrEmpty(pid);
            fileSvr.pidRoot = pidRoot;
            fileSvr.nameLoc = Path.GetFileName(pathLoc);
            fileSvr.pathLoc = pathLoc;
            fileSvr.pathRel = PathTool.combin(pathRel, fileSvr.nameLoc);
            fileSvr.lenLoc  = Convert.ToInt64(lenLoc);
            fileSvr.sizeLoc = sizeLoc;
            fileSvr.deleted = false;
            fileSvr.md5     = md5;
            fileSvr.nameSvr = fileSvr.nameLoc;

            //同名文件检测
            //DbFolder df = new DbFolder();
            //if (df.exist_same_file(fileSvr.nameLoc, pid))
            //{
            //    var data = callback + "({'value':'','ret':false,'code':'101'})";
            //    this.toContent(data);
            //    return;
            //}

            //所有单个文件均以uuid/file方式存储
            PathBuilderUuid pb = new PathBuilderUuid();

            fileSvr.pathSvr = pb.genFile(fileSvr.uid, ref fileSvr);
            fileSvr.pathSvr = fileSvr.pathSvr.Replace("\\", "/");

            //数据库存在相同文件
            DBConfig cfg       = new DBConfig();
            DBFile   db        = cfg.db();
            FileInf  fileExist = new FileInf();

            if (db.exist_file(md5, ref fileExist))
            {
                fileSvr.nameSvr  = fileExist.nameSvr;
                fileSvr.pathSvr  = fileExist.pathSvr;
                fileSvr.perSvr   = fileExist.perSvr;
                fileSvr.lenSvr   = fileExist.lenSvr;
                fileSvr.complete = fileExist.complete;
                db.Add(ref fileSvr);

                //触发事件
                up6_biz_event.file_create_same(fileSvr);
            }//数据库不存在相同文件
            else
            {
                db.Add(ref fileSvr);
                //触发事件
                up6_biz_event.file_create(fileSvr);

                //2.0创建器。仅创建一个空白文件
                FileBlockWriter fr = new FileBlockWriter();
                fr.make(fileSvr.pathSvr, fileSvr.lenLoc);
            }

            //加密
            ConfigReader cr      = new ConfigReader();
            var          sec     = cr.module("path");
            var          encrypt = (bool)sec.SelectToken("$.security.encrypt");

            if (encrypt)
            {
                CryptoTool ct = new CryptoTool();
                fileSvr.pathSvr = ct.encode(fileSvr.pathSvr);
            }

            string jv = JsonConvert.SerializeObject(fileSvr);

            jv = HttpUtility.UrlEncode(jv);
            jv = jv.Replace("+", "%20");
            string json = callback + "({\"value\":\"" + jv + "\",\"ret\":true})";//返回jsonp格式数据。

            this.toContent(json);
        }
Exemplo n.º 9
0
        protected void Page_Load(object sender, EventArgs e)
        {
            string op       = this.reqString("op");
            string pid      = this.reqString("pid");
            string pidRoot  = this.reqString("pidRoot");
            string md5      = this.reqString("md5");
            string id       = this.reqString("id");
            string uid      = this.reqString("uid");
            string lenLoc   = this.reqString("lenLoc");
            string sizeLoc  = this.reqString("sizeLoc");
            string token    = this.reqString("token");
            string callback = this.reqString("callback");     //jsonp参数
            //客户端使用的是encodeURIComponent编码,
            string pathLoc = this.reqStringDecode("pathLoc"); //utf-8解码

            if (op == "mkpath")
            {
                this.mkpath();
            }

            if (string.IsNullOrEmpty(pid))
            {
                pid = string.Empty;
            }
            if (string.IsNullOrEmpty(pidRoot))
            {
                pidRoot = pid;
            }

            //参数为空
            if (string.IsNullOrEmpty(md5) ||
                string.IsNullOrEmpty(uid) ||
                string.IsNullOrEmpty(sizeLoc)
                )
            {
                Response.Write(callback + "({\"value\":null})");
                return;
            }

            FileInf fileSvr = new FileInf();

            fileSvr.fdChild = false;
            fileSvr.uid     = int.Parse(uid);//将当前文件UID设置为当前用户UID
            fileSvr.id      = id;
            fileSvr.pid     = pid;
            fileSvr.fdChild = !string.IsNullOrEmpty(pid);
            fileSvr.pidRoot = pidRoot;
            fileSvr.nameLoc = Path.GetFileName(pathLoc);
            fileSvr.pathLoc = pathLoc;
            fileSvr.lenLoc  = Convert.ToInt64(lenLoc);
            fileSvr.sizeLoc = sizeLoc;
            fileSvr.deleted = false;
            fileSvr.md5     = md5;
            fileSvr.nameSvr = fileSvr.nameLoc;

            WebSafe ws  = new WebSafe();
            var     ret = ws.validToken(token, fileSvr);

            //token验证失败
            if (!ret)
            {
                string m = callback + "({\"value\":\"0\",\"ret\":false,\"msg\":\"token error\"})";//返回jsonp格式数据。
                this.toContentJson(m);
                return;
            }

            //所有单个文件均以uuid/file方式存储
            PathBuilderUuid pb = new PathBuilderUuid();

            fileSvr.pathSvr = pb.genFile(fileSvr.uid, ref fileSvr);
            fileSvr.pathSvr = fileSvr.pathSvr.Replace("\\", "/");

            //数据库存在相同文件
            DBConfig cfg       = new DBConfig();
            DBFile   db        = cfg.db();
            FileInf  fileExist = new FileInf();

            if (db.exist_file(md5, ref fileExist))
            {
                fileSvr.nameSvr  = fileExist.nameSvr;
                fileSvr.pathSvr  = fileExist.pathSvr;
                fileSvr.perSvr   = fileExist.perSvr;
                fileSvr.lenSvr   = fileExist.lenSvr;
                fileSvr.complete = fileExist.complete;
                db.Add(ref fileSvr);

                //触发事件
                up6_biz_event.file_create_same(fileSvr);
            }//数据库不存在相同文件
            else
            {
                db.Add(ref fileSvr);
                //触发事件
                up6_biz_event.file_create(fileSvr);

                //2.0创建器。仅创建一个空白文件
                FileBlockWriter fr = new FileBlockWriter();
                fr.make(fileSvr.pathSvr, fileSvr.lenLoc);
            }

            //将路径转换成相对路径
            fileSvr.pathSvr = pb.absToRel(fileSvr.pathSvr);
            //加密
            ConfigReader cr      = new ConfigReader();
            var          sec     = cr.module("path");
            var          encrypt = (bool)sec.SelectToken("$.security.encrypt");

            if (encrypt)
            {
                CryptoTool ct = new CryptoTool();
                fileSvr.pathSvr = ct.encode(fileSvr.pathSvr);
            }

            string jv = JsonConvert.SerializeObject(fileSvr);

            jv = HttpUtility.UrlEncode(jv);
            jv = jv.Replace("+", "%20");
            string json = callback + "({\"value\":\"" + jv + "\",\"ret\":true})";//返回jsonp格式数据。

            this.toContentJson(json);
        }
Exemplo n.º 10
0
        /// <summary>
        /// 只负责拼接文件块。将接收的文件块数据写入到文件中。
        /// 更新记录:
        ///		2012-04-12 更新文件大小变量类型,增加对2G以上文件的支持。
        ///		2012-04-18 取消更新文件上传进度信息逻辑。
        ///		2012-10-30 增加更新文件进度功能。
        ///		2015-03-19 文件路径由客户端提供,此页面不再查询文件在服务端的路径。减少一次数据库访问操作。
        ///     2016-03-31 增加文件夹信息字段
        ///     2017-07-11 优化参数检查逻辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            string uid         = this.headString("uid");
            string f_id        = this.headString("id");
            string lenSvr      = this.headString("lenSvr");     //已传大小
            string lenLoc      = this.headString("lenLoc");     //本地文件大小
            string blockOffset = this.headString("blockOffset");
            string blockSize   = this.headString("blockSize");  //当前块大小
            string blockIndex  = this.headString("blockIndex"); //当前块索引,基于1
            string blockMd5    = this.headString("blockMd5");   //块MD5
            string complete    = this.headString("complete");   //true/false
            string pathSvr     = Request.Form["pathSvr"];       //
            string pathLoc     = string.Empty;
            string token       = this.headString("token");      //

            pathSvr = Server.UrlDecode(pathSvr);

            if (!this.safe_check(lenLoc, uid, f_id, blockOffset, pathSvr))
            {
                return;
            }


            //有文件块数据
            if (Request.Files.Count > 0)
            {
                bool           verify = false;
                string         msg    = string.Empty;
                string         md5Svr = string.Empty;
                HttpPostedFile file   = Request.Files.Get(0);//文件块
                var            stm    = file.InputStream;
                var            stmLen = int.Parse(blockSize);
                pathLoc = file.FileName;

                //加密
                ConfigReader cr      = new ConfigReader();
                var          sec     = cr.module("path");
                var          encrypt = (bool)sec.SelectToken("$.security.encrypt");
                if (encrypt)
                {
                    CryptoTool ct = new CryptoTool();
                    pathSvr = ct.decode(pathSvr);
                    stm     = ct.decode(file.InputStream, int.Parse(blockSize));
                }

                //token验证
                WebSafe ws      = new WebSafe();
                FileInf fileSvr = new FileInf();
                fileSvr.id      = f_id;
                fileSvr.pathLoc = file.FileName;
                fileSvr.pathSvr = pathSvr;
                FileInfo fi = new FileInfo(pathLoc);
                fileSvr.nameLoc = fi.Name;
                verify          = ws.validToken(token, fileSvr, "block");
                //token验证失败
                if (!verify)
                {
                    msg = string.Format("token error loc:{0}", token);
                }

                //计算文件块MD5
                if (!string.IsNullOrEmpty(blockMd5))
                {
                    md5Svr = Md5Tool.calc(stm);
                }

                //文件块大小验证
                if (verify)
                {
                    verify = int.Parse(blockSize) == stm.Length;
                }
                if (!verify)
                {
                    msg = "block size error sizeSvr:" + stm.Length + " sizeLoc:" + blockSize;
                }

                //块MD5验证
                if (verify && !string.IsNullOrEmpty(blockMd5))
                {
                    verify = md5Svr == blockMd5;
                    if (!verify)
                    {
                        msg = "block md5 error";
                    }
                }

                if (verify)
                {
                    PathBuilder pb = new PathBuilder();
                    pathSvr = pb.relToAbs(pathSvr);
                    //2.0保存文件块数据
                    FileBlockWriter res = new FileBlockWriter();
                    res.make(pathSvr, Convert.ToInt64(lenLoc));
                    res.write(pathSvr, Convert.ToInt64(blockOffset), stm);
                    up6_biz_event.file_post_block(f_id, Convert.ToInt32(blockIndex));

                    //生成信息
                    JObject o = new JObject();
                    o["msg"]    = "ok";
                    o["md5"]    = md5Svr;      //文件块MD5
                    o["offset"] = blockOffset; //偏移
                    msg         = JsonConvert.SerializeObject(o);
                }
                this.toContentJson(msg);
            }
        }