コード例 #1
0
ファイル: RestApi.cs プロジェクト: oonqt/SafeDelete
        public object Post(SafeDeleteAction request)
        {
            _logger.Info("POST SafeDeleteAction: {0}", request);

            long     item_id = long.Parse(request.item_id);
            BaseItem item    = _lm.GetItemById(item_id);

            List <FileSystemMetadata> del_paths = item.GetDeletePaths(false);
            PathWalker walker = new PathWalker(del_paths, _fs);

            List <KeyValuePair <string, long> > file_list  = walker.GetFileNames();
            Dictionary <string, int>            ext_counts = walker.GetExtCounts();

            // verify action token
            string file_info_string = "";
            string file_list_hash   = "";

            foreach (var file_item in file_list)
            {
                file_info_string += file_item.Key + "|" + file_item.Value + "|";
            }
            using (MD5 md5Hash = MD5.Create())
            {
                byte[]        hashBytes = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(file_info_string));
                StringBuilder sb        = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    sb.Append(hashBytes[i].ToString("X2"));
                }
                file_list_hash = sb.ToString();
            }

            Dictionary <string, object> result = new Dictionary <string, object>();

            if (file_list_hash != request.action_token)
            {
                result.Add("result", false);
                result.Add("message", "The action tokens to not match.");
                return(result);
            }

            // do file move
            string recycle_path = Plugin.Instance.Configuration.RecycledPath;

            if (string.IsNullOrEmpty(recycle_path))
            {
                result.Add("result", false);
                result.Add("message", "Recycle path not set.");
                return(result);
            }

            if (!_fs.DirectoryExists(recycle_path))
            {
                result.Add("result", false);
                result.Add("message", "Recycle path does not exist.");
                return(result);
            }

            // do file moves
            string time_stamp  = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff");
            int    item_number = 0;

            bool   action_result  = true;
            string action_message = "Files moved.";

            foreach (var del_item in del_paths)
            {
                _logger.Info("Item Delete Path: " + del_item.FullName);

                if (del_item.IsDirectory)
                {
                    FileSystemMetadata fsm         = _fs.GetDirectoryInfo(del_item.FullName);
                    string             destination = System.IO.Path.Combine(recycle_path, time_stamp, item_number.ToString());
                    _logger.Info("Item Delete Destination Path: " + destination);

                    try
                    {
                        _fs.CreateDirectory(destination);
                        destination = System.IO.Path.Combine(destination, fsm.Name);
                        _fs.MoveDirectory(del_item.FullName, destination);
                    }
                    catch (Exception e)
                    {
                        action_result  = false;
                        action_message = e.Message;
                    }
                }
                else
                {
                    FileSystemMetadata fsm         = _fs.GetDirectoryInfo(del_item.FullName);
                    string             destination = System.IO.Path.Combine(recycle_path, time_stamp, item_number.ToString());
                    _logger.Info("Item Delete Destination Path: " + destination);

                    try
                    {
                        _fs.CreateDirectory(destination);
                        destination = System.IO.Path.Combine(destination, fsm.Name);
                        _fs.MoveFile(del_item.FullName, destination);
                    }
                    catch (Exception e)
                    {
                        action_result  = false;
                        action_message = e.Message;
                    }
                }

                if (!action_result)
                {
                    break;
                }

                item_number++;
            }

            result.Add("result", action_result);
            result.Add("message", action_message);

            return(result);
        }
コード例 #2
0
ファイル: RestApi.cs プロジェクト: oonqt/SafeDelete
        public object Get(SafeDelete request)
        {
            _logger.Info("GET SafeDelete: {0}", request);

            long     item_id = long.Parse(request.item_id);
            BaseItem item    = _lm.GetItemById(item_id);

            List <FileSystemMetadata> del_paths = item.GetDeletePaths(false);

            foreach (var del_item in del_paths)
            {
                _logger.Info("Item Delete Path: " + del_item.FullName);
            }

            PathWalker walker = new PathWalker(del_paths, _fs);

            foreach (var path_item in walker.GetFileList())
            {
                _logger.Info("Item Delete Walked Path: " + path_item.FullName);
            }

            List <KeyValuePair <string, long> > file_list  = walker.GetFileNames();
            Dictionary <string, int>            ext_counts = walker.GetExtCounts();

            // calculate delete token based on files and sizes
            string file_info_string = "";
            string file_list_hash   = "";

            foreach (var file_item in file_list)
            {
                file_info_string += file_item.Key + "|" + file_item.Value + "|";
            }
            using (MD5 md5Hash = MD5.Create())
            {
                byte[]        hashBytes = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(file_info_string));
                StringBuilder sb        = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    sb.Append(hashBytes[i].ToString("X2"));
                }
                file_list_hash = sb.ToString();
            }

            Dictionary <string, object> results = new Dictionary <string, object>();

            results.Add("file_list", file_list);
            results.Add("ext_counts", ext_counts);
            results.Add("action_token", file_list_hash);

            foreach (var file_item in file_list)
            {
                _logger.Info("Item Delete Walked Files: " + file_item.Key + " - " + file_item.Value);
            }

            foreach (KeyValuePair <string, int> ext_count in ext_counts)
            {
                _logger.Info("Item Delete Walked Ext: " + ext_count.Key + " - " + ext_count.Value);
            }

            return(results);
        }