Exemple #1
0
        public void Test1()
        {
            String       str = "[{\"isDelete\":0,\"objuuid\":\"y9zGSfaAwbsHYBjDAg4PdA\",\"uuid\":51,\"table\":\"mainSync\",\"data\":\"\",\"id\":51}]";
            List <DBObj> obj = DBObj.FromJsonLst(str);

            Assert.AreEqual(0, obj[0].isDelete);
            Assert.AreEqual("y9zGSfaAwbsHYBjDAg4PdA", obj[0].objuuid);
            Assert.AreEqual(51, obj[0].id);
        }
        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));
        }