Ejemplo n.º 1
0
        public static string ResponseGET200ToJSON(string fileName, string filePath)
        {
            Byte[] bytes   = File.ReadAllBytes(filePath);
            String content = Convert.ToBase64String(bytes);

            string name     = Base64.Base64Encode(fileName);
            string checksum = Checksums.GetSha1Hash(filePath);

            string str = "RESPONSE {";

            str += " 'status': '200',";
            str += " 'filename': '" + name + "',";
            str += " 'content': '" + content + "',";
            str += " 'checksum': '" + checksum;
            str += "'}";

            return(str);
        }
Ejemplo n.º 2
0
        public static string PutFile(string path, string data)
        {
            string cleanData = SplitString(data);

            FileHandler file = JsonConvert.DeserializeObject <FileHandler>(cleanData);

            string fileName = Base64.Base64Decode(file.FileName);

            Console.WriteLine("Check " + fileName);

            string[]      fileEntries = Directory.GetFiles(path);
            string        response    = null;
            List <string> fileNames   = new List <string>();

            foreach (var entry in fileEntries)
            {
                string name = Path.GetFileName(entry);
                fileNames.Add(name);
            }

            if (fileNames.Contains(fileName))
            {
                string checksum = Checksums.GetSha1Hash(path + @"\" + fileName);

                if (checksum == file.OriginalChecksum)
                {
                    File.WriteAllBytes(path + @"\" + fileName, Convert.FromBase64String(file.Content));
                    response = FileHandler.ResponsePUT200ToJson();
                }
                else
                {
                    response = FileHandler.ResponsePUT412ToJson();
                }
            }
            else
            {
                var createFile = File.Create(path + @"\" + fileName);
                createFile.Close();

                File.WriteAllBytes(path + @"\" + fileName, Convert.FromBase64String(file.Content));
                response = FileHandler.ResponsePUT200ToJson();
            }
            return(response);
        }
Ejemplo n.º 3
0
        public static string DeleteFile(string path, string data)
        {
            string cleanData = SplitString(data);

            FileHandler file = JsonConvert.DeserializeObject <FileHandler>(cleanData);

            string fileName     = Base64.Base64Decode(file.FileName);
            string fileChecksum = file.Checksum;

            Console.WriteLine("Check " + fileName);

            string[]      fileEntries = Directory.GetFiles(path);
            string        response    = null;
            List <string> fileNames   = new List <string>();

            foreach (var entry in fileEntries)
            {
                string name = Path.GetFileName(entry);
                fileNames.Add(name);
            }
            string filePath = path + @"\" + fileName;

            if (fileNames.Contains(fileName))
            {
                string checksum = Checksums.GetSha1Hash(filePath);
                if (checksum == fileChecksum)
                {
                    File.Delete(filePath);
                    response = FileHandler.ResponseDELETE200ToJSON();
                }
                else
                {
                    response = FileHandler.ResponseDELETE412ToJSON();
                }
            }
            else
            {
                response = FileHandler.ResponseDELETE404ToJSON();
            }

            return(response);
        }
Ejemplo n.º 4
0
        public static string ResponseLIST200ToJSON(string path)
        {
            string str = "RESPONSE {";

            str += " 'status': '200',";
            str += " 'files': [";

            DirectoryInfo c = new DirectoryInfo(path);

            FileInfo[] Files2 = c.GetFiles("*.*");
            var        count  = Files2.Length;

            foreach (FileInfo file in Files2)
            {
                FileHandler tempFile = new FileHandler();
                string      name     = file.Name;
                string      name2    = Base64.Base64Encode(name);
                string      checksum = Checksums.GetSha1Hash(path + @"\" + name);
                str += "{";
                str += "'filename': '" + name2 + "',";
                str += " 'checksum': '" + checksum + "'";
                if (--count > 0)
                {
                    str += "},";
                }
                else
                {
                    str += "}";
                }
                //string str1 = "{ 'filename': '" + name2 + "', 'checksum': '" + checksum + "'},";

                //str += str1;
            }

            str += "] }";
            return(str);
        }