FCExporter.setupServer C# (CSharp) Method

setupServer() private method

check server permissions and settings and return ready flag to exportSettings
private setupServer ( Hashtable exportParams ) : Hashtable
exportParams Hashtable Various export parameters
return Hashtable
    private Hashtable setupServer(Hashtable exportParams)
    {

        //get export file name
        string exportFile = exportParams["exportfilename"].ToString();
        // get extension related to specified type 
        string ext = getExtension(exportParams["exportformat"].ToString());

        Hashtable retServerStatus = new Hashtable();
        
        //set server status to true by default
        retServerStatus["ready"] = true;

        // Open a FileStream to be used as outpur stream when the file would be saved
        FileStream fos;

        // process SAVE_PATH : the path where export file would be saved
        // add a / at the end of path if / is absent at the end

        string path = SAVE_PATH;
        // if path is null set it to folder where FCExporter.aspx is present
        if (path.Trim() == "") path = "./";
        path = Regex.Replace(path, @"([^\/]$)", "${1}/");

        try
        {
            // check if the path is relative if so assign the actual path to path
            path = HttpContext.Current.Server.MapPath(path);
        }
        catch (HttpException e)
        {
            raise_error(e.Message);
        }


        // check whether directory exists
        // raise error and halt execution if directory does not exists
        if (!Directory.Exists(path)) raise_error(" Server Directory does not exist.", true);

        // check if directory is writable or not
        bool dirWritable = isDirectoryWritable(path);

        // build filepath
        retServerStatus["filepath"] = exportFile + "." + ext;

        // check whether file exists
        if (!File.Exists(path + retServerStatus["filepath"].ToString()))
        {
            // need to create a new file if does not exists
            // need to check whether the directory is writable to create a new file  
            if (dirWritable)
            {
                // if directory is writable return with ready flag

                // open the output file in FileStream
                fos = File.Open(path + retServerStatus["filepath"].ToString(), FileMode.Create, FileAccess.Write);

                // set the output stream to the FileStream object
                retServerStatus["outStream"] = fos;
                return retServerStatus;
            }
            else
            {
                // if not writable halt and raise error
                raise_error("403", true);
            }
        }

        // add notice that file exists 
        raise_error(" File already exists.");

        //if overwrite is on return with ready flag 
        if (OVERWRITEFILE)
        {
            // add notice while trying to overwrite
            raise_error(" Export handler's Overwrite setting is on. Trying to overwrite.");

            // see whether the existing file is writable
            // if not halt raising error message
            if ((new FileInfo(path + retServerStatus["filepath"].ToString())).IsReadOnly)
                raise_error(" Overwrite forbidden. File cannot be overwritten.", true);

            // if writable return with ready flag 
            // open the output file in FileStream
            // set the output stream to the FileStream object
            fos = File.Open(path + retServerStatus["filepath"].ToString(), FileMode.Create, FileAccess.Write);
            retServerStatus["outStream"] = fos;
            return retServerStatus;
        }

        // raise error and halt execution when overwrite is off and intelligent naming is off 
        if (!INTELLIGENTFILENAMING)
        {
            raise_error(" Export handler's Overwrite setting is off. Cannot overwrite.", true);
        }

        raise_error(" Using intelligent naming of file by adding an unique suffix to the exising name.");
        // Intelligent naming 
        // generate new filename with additional suffix
        exportFile = exportFile + "_" + generateIntelligentFileId();
        retServerStatus["filepath"] = exportFile + "." + ext;

        // return intelligent file name with ready flag
        // need to check whether the directory is writable to create a new file  
        if (dirWritable)
        {
            // if directory is writable return with ready flag
            // add new filename notice
            // open the output file in FileStream
            // set the output stream to the FileStream object
            raise_error(" The filename has changed to " + retServerStatus["filepath"].ToString() + ".");
            fos = File.Open(path + retServerStatus["filepath"].ToString(), FileMode.Create, FileAccess.Write);

            // set the output stream to the FileStream object
            retServerStatus["outStream"] = fos;
            return retServerStatus;
        }
        else
        {
            // if not writable halt and raise error
            raise_error("403", true);
        }

        // in any unknown case the export should not execute	
        retServerStatus["ready"] = false;
        raise_error(" Not exported due to unknown reasons.");
        return retServerStatus;

    }
    /// <summary>