<%@ WebService Language="C#" Class="AdministrationService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using System.Collections.Generic;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class AdministrationService  : System.Web.Services.WebService 
{    

    [WebMethod]
    public string[] GetRootFolders()
    {
        try
        {
            string[] directories = System.IO.Directory.GetDirectories(Server.MapPath("/"));
            List<string> dirs = new List<string>();
            foreach (string directory in directories)
            {
                DirectoryInfo info = new DirectoryInfo(directory);
                dirs.Add(info.Name);
            }
            return dirs.ToArray();
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    [WebMethod]
    public string[] GetSubFolders(string folder)
    {
        try
        {
            string[] directories = System.IO.Directory.GetDirectories(Server.MapPath("/") + "/" + folder);
            List<string> dirs = new List<string>();
            foreach (string directory in directories)
            {
                DirectoryInfo info = new DirectoryInfo(directory);
                dirs.Add(info.Name);
            }
            return dirs.ToArray();
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    [WebMethod]
    public string[] GetFolderDocuments(string folder)
    {
        try
        {
            string[] filesList = System.IO.Directory.GetFiles(Server.MapPath("/") + "/" + folder);
            List<string> files = new List<string>();
            foreach (string file in filesList)
            {
                FileInfo info = new FileInfo(file);
                files.Add(info.Name);
            }
            return files.ToArray();
        }
        catch (Exception ex)
        {
            return null;
        }
            
    }

    [WebMethod]
    public string[] CreateFolder(string folder)
    {
        try
        {
            DirectoryInfo info = Directory.CreateDirectory(Server.MapPath("/") + "/" + folder);
            DirectoryInfo[] directories = info.Parent.GetDirectories();
            List<string> dirs = new List<string>();
            foreach (DirectoryInfo directory in directories)
            {

                dirs.Add(directory.Name);
            }
            return dirs.ToArray();
        }
        catch (Exception ex)
        {
            return null;
        }   
    }
    
    [WebMethod]
    public string GetDocument(string documentName, string folder)
    {
        try
        {
            string file = Server.MapPath("/");
            if (folder == "/" || folder.Length == 0)
            {
                file += documentName;
            }
            else
            {
                file += folder + "/" + documentName;
            }
            string fileText = File.ReadAllText(file);
            return fileText;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    [WebMethod]
    public bool UploadDocument(string documentName, byte[] data, string folder)
    {
        try
        {
            string file = Server.MapPath("/");
            if (folder == "/" || folder.Length == 0)
            {
                file += documentName;
            }
            else
            {
                file += folder + "/" + documentName;
            }
            File.WriteAllBytes(file, data);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    [WebMethod]
    public bool DeleteDocument(string documentName, string folder)
    {
        try
        {
            string file = Server.MapPath("/");
            if (folder == "/" || folder.Length == 0)
            {
                file += documentName;
            }
            else
            {
                file += folder + "/" + documentName;
            }
            File.Delete(file);            
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    
}

