Esempio n. 1
0
        public IActionResult Index(String code, String DBName, int beginId, int take,
                                   String uuid, String isWait)
        {
            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            lastId = 0L;
            var            locker = SqliteLock.getLockObj(path);
            String         dbUUid = null;
            DownloadResult dr     = new DownloadResult();

            lock (locker)
            {
                dbUUid = OpSqlite.getQuickSaveValue("uuid", path);
                lastId = OpSqlite.getMaxID(path) ?? 0;
                LastID.setLastId(path, lastId);
                dr.maxId = lastId;
                if (dbUUid != (uuid) && !string.IsNullOrEmpty(uuid))
                {
                    if (lastId == 0)
                    {
                        dr.status = (-2);
                    }
                    else
                    {
                        dr.status = (-1);
                    }
                }
                else if (beginId - 1 > lastId)
                {
                    dr.status = (-2);
                }
            }

            if (dr.status != -1 && dr.status != -2)
            {
                int jsq = 0;
                while (lastId != 0 && lastId < (long)beginId && jsq < 120 && "1" == (isWait))
                {
                    Thread.Sleep(500);
                    lastId = LastID.getLastId(path) ?? 0;
                    jsq++;
                }
                lock (locker)
                {
                    dr.status = (1);
                    List <DBObj> db = OpSqlite.SelectDBObj(beginId, take, path);
                    dr.uuid  = (dbUUid);
                    dr.data  = (db);
                    dr.maxId = (lastId);
                    if (db.Count > 0)
                    {
                        List <long> dels = OpSqlite.SelectDelIds(db[(0)].id, db[(db.Count - 1)].id, path);
                        dr.delIds = (dels);
                    }
                }
            }

            return(Json(dr));
        }
        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));
        }