public IActionResult Index(String code, String DBName, long localMaxId,
                                   String uuid, String Data, String restore, String localUUID, String uploadUUID)
        {
            var filePath = Configs.uploadFilePath;

            filePath = _hostingEnvironment.WebRootPath.TrimEnd('\\', '/')
                       + "/" + filePath.TrimEnd('\\', '/') + "/";
            System.IO.Directory.CreateDirectory(filePath);

            Response.ContentType = "text/plain;charset=utf-8";
            Response.Headers.Add("Access-Control-Allow-Origin", "*");
            Response.Headers.Add("Access-Control-Allow-Methods", "POST");
            Response.Headers.Add("Access-Control-Allow-Headers", "x-requested-with,content-type");

            String ml = filePath + "/" + code + "/";

            System.IO.Directory.CreateDirectory(ml);

            String       path   = filePath + "/" + code + "/" + DBName + ".db";
            var          locker = SqliteLock.getLockObj(path);
            long         lastID = 0L;
            UploadResult resObj = new UploadResult();

            lock (locker)
            {
                lastID = OpSqlite.getMaxID(path) ?? 0;
                String dbUUid = OpSqlite.getQuickSaveValue("uuid", path);

                resObj.status = (-9);
                resObj.uuid   = (dbUUid);
                var iswrite     = true;
                var isUUIDMatch = dbUUid == (uuid) || string.IsNullOrEmpty(uuid);
                if (!isUUIDMatch || lastID < localMaxId)
                {
                    if ((isUUIDMatch && lastID < localMaxId) || (!isUUIDMatch && (lastID == 0L) && "1" != (restore)))
                    {
                        resObj.status = (-2);
                        resObj.lastId = (lastID);
                        iswrite       = false;
                    }
                    else if ("1" == (restore) && (lastID == 0))
                    {
                        OpSqlite.QuickSave("uuid", uuid, path);
                    }
                    else
                    {
                        resObj.status = (-1);
                        iswrite       = false;
                    }
                }
                if (iswrite)
                {
                    List <DBObj> db = DBObj.FromJsonLst(Data);
                    if (db != null && db.Count > 0)
                    {
                        long insertResult = OpSqlite.InsertDBObjLst(db, path, localUUID, uploadUUID, "1" == (restore));
                        if (insertResult != 0)
                        {
                            resObj.status = (1);
                            for (int i = 0; i < db.Count; i++)
                            {
                                db[(i)].data = ("");
                                if ("1" != (restore))
                                {
                                    db[(i)].id = (insertResult++);
                                }
                            }
                            resObj.data = (db);
                        }
                        if ("1" == (restore))
                        {
                            lastID = OpSqlite.getMaxID(path) ?? 0;
                        }
                        else
                        {
                            lastID = insertResult - 1;
                        }
                    }
                }
            }
            if (lastID != 0L)
            {
                LastID.setLastId(path, lastID);
            }
            new Thread(async delegate()
            {
                await WebSocketHandler.sendMsg(DBName, code);
            }).Start();

            return(Json(resObj));
        }
示例#2
0
        public void Test2()
        {
            var filepath = "2.db";

            try
            {
                File.Delete(filepath);
            }
            catch { }
            var localGUID  = "localGUID1";
            var uploadGUID = 1;
            var data       = new List <DBObj>()
            {
                new DBObj()
                {
                    id       = 1,
                    objuuid  = "o1",
                    uuid     = "u1",
                    data     = "v1",
                    isDelete = 0
                },
                new DBObj()
                {
                    id       = 2,
                    objuuid  = "o2",
                    uuid     = "u2",
                    data     = "v2",
                    isDelete = 0
                }
            };

            Assert.AreEqual(0, OpSqlite.getMaxID(filepath) ?? 0);

            var res = OpSqlite.SelectDBObj(0, 100, filepath);

            //Assert.AreEqual(0, OpSqlite.getMaxID(filepath) ?? 0);
            OpSqlite.InsertDBObjLst(data, filepath, localGUID, uploadGUID++.ToString(), false);
            Assert.AreEqual(2, OpSqlite.getMaxID(filepath) ?? 0);
            res = OpSqlite.SelectDBObj(0, 100, filepath);
            Assert.AreEqual(2, res.Count);
            Assert.AreEqual("o1", res[0].objuuid);
            Assert.AreEqual("v2", res[1].data);

            var data2 = new List <DBObj>()
            {
                new DBObj()
                {
                    id       = 1,
                    objuuid  = "o1",
                    uuid     = "u1",
                    data     = "v1",
                    isDelete = 1
                }
            };

            OpSqlite.InsertDBObjLst(data2, filepath, localGUID, uploadGUID++.ToString(), false);

            res = OpSqlite.SelectDBObj(0, 100, filepath);

            Assert.AreEqual(1, res.First(f => f.objuuid == "o1").isDelete);

            Assert.AreEqual(3, OpSqlite.getMaxID(filepath).Value);

            Assert.AreEqual(1, OpSqlite.SelectDelIds(0, 100, filepath)[0]);
        }