コード例 #1
0
ファイル: Form2.cs プロジェクト: bsankara/FileSync
        public Form2(B2SDK sdk)
        {
            InitializeComponent();
            authorizedSDK = sdk;
            SqlQuery dbConnection = new SqlQuery(dbName);
            if (System.IO.File.Exists(dbName))
            {
                string bucketName = dbConnection.getPrefferedBucketName();
                txtBucketName.Text = bucketName;

                string directoryName = dbConnection.getDirectoryName();
                txtFolderToSync.Text = directoryName;
            }
        }
コード例 #2
0
ファイル: Form2.cs プロジェクト: bsankara/FileSync
        private void btnStartSync_Click(object sender, EventArgs e)
        {
            string storageBucketName = txtBucketName.Text;
            SqlQuery dbConnection = new SqlQuery(dbName);
            if (!System.IO.File.Exists(dbName))
            {
                dbConnection.createAndInitializeDatabase();
            }
            string fileSyncDirectory = txtFolderToSync.Text;
            dbConnection.savePreferredBucketName(storageBucketName);
            if (Directory.Exists(fileSyncDirectory))
            {
                string bucketId = getOrCreateBucketId(storageBucketName);
                Dictionary<string, string> processedFileList = processFilesInBucket(bucketId);
                dbConnection.addDirectoryToFileSyncDirectory(fileSyncDirectory);
                string[] files = Directory.GetFiles(fileSyncDirectory, "*", SearchOption.AllDirectories);
                // disable buttons

                btnPullData.Enabled = false;
                btnPushData.Enabled = false;

                foreach (string fileName in files)
                {
                    // backblaze doesnt allow \'s so we have to replace with /'s .... we'll have to do this the other way around
                    // thankfully windows doesnt let you name files with that so it will only denominate folder separations.
                    string cleanFileName = fileName.Replace(@"\", "/");
                    byte[] fileBytes = File.ReadAllBytes(fileName);
                    string sha1Hash = calculateSHA1(fileBytes);
                    if (processedFileList.ContainsKey(cleanFileName))
                    {
                        B2FileInfo fileInfo = authorizedSDK.b2_get_file_info(processedFileList[cleanFileName]);
                        if (fileInfo.contentSha1.Equals(sha1Hash))
                        {
                            // do nothing
                        }
                        else
                        {
                            authorizedSDK.b2_delete_file_version(cleanFileName, processedFileList[cleanFileName]);
                            fileInfo = authorizedSDK.b2_upload_file(fileBytes, cleanFileName, bucketId);
                            TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
                            int secondsSinceEpoch = (int)t.TotalSeconds;
                            if (fileInfo.contentSha1 == sha1Hash)
                            {
                                dbConnection.addSyncedFileInfo(cleanFileName, secondsSinceEpoch, sha1Hash);
                            }
                        }
                    }
                    else
                    {
                        B2FileInfo fileInfo = authorizedSDK.b2_upload_file(fileBytes, cleanFileName, bucketId);
                        TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
                        int secondsSinceEpoch = (int)t.TotalSeconds;
                        if (fileInfo.contentSha1 == sha1Hash)
                        {
                            dbConnection.addSyncedFileInfo(cleanFileName, secondsSinceEpoch, sha1Hash);
                        }

                    }
                }
            }
            else
            {
                //Invalid directory
            }

            btnPullData.Enabled = true;
            btnPushData.Enabled = true;
        }