1. Home
  2. Data
  3. File Storage Services
  4. Storage – Local Implementation

Storage – Local Implementation

Configuration

Local Implementation supports the Local File and Local File PRIVATE storage types. To setup a connection to this service you need a URL which is the root URL to access the files, Primary Directory that indicates where the files will be saved, and Backup Directory if the system requires the files be in a backup location.

Implementation

Save

public static void SaveLocalFile(StorageInformation storageInformation)
{
    try
    {
        // Save the provided file in the primary location and make it accessible via a URL...
        XElement storageXML = XElement.Parse(storageInformation.StorageDefinition);
        string PrimaryDirectory = $"{storageXML.GetElement("PrimaryDirectory", "")}{storageInformation.WorkspaceGUID}\\";
        string URL = storageXML.GetElement("URL", "");

        DirectoryInfo MyPrimaryDir = new(PrimaryDirectory);
        if (!MyPrimaryDir.Exists)
        {
            Directory.CreateDirectory(PrimaryDirectory);
        }

        string fileType = storageInformation.IsFileCompressed ? "zip" : $"{storageInformation.FileType.ToLower()}";
        FileStream MyPrimaryFile = new($"{PrimaryDirectory}{storageInformation.FileName.ToLower()}.{fileType}"
                                       , FileMode.Create
                                       , FileAccess.Write
                                      );
        storageInformation.MemoryStream.Position = 0;
        storageInformation.MemoryStream.WriteTo(MyPrimaryFile);
        MyPrimaryFile.Close();

        if (storageInformation.IsFilePublic)
        {
            storageInformation.FileURL = $"{URL}{storageInformation.WorkspaceGUID}/{storageInformation.FileName.ToLower()}.{fileType}";
        }
        else
        {
            storageInformation.FileURL = $"PRIVATE:{URL}{storageInformation.WorkspaceGUID}/{storageInformation.FileName.ToLower()}.{fileType}";
        }

        // IF the system needs a backup copy of the file in a different location
        if (!string.IsNullOrEmpty(storageXML.GetElement("BackupDirectory", "")))
        {
            try
            {
                string BackupDirectory = $"{storageXML.GetElement("BackupDirectory", "")}{storageInformation.WorkspaceGUID}\\";

                DirectoryInfo MyBackupDir = new(BackupDirectory);
                if (!MyBackupDir.Exists)
                {
                    Directory.CreateDirectory(BackupDirectory);
                }

                FileStream MyBackupFile = new($"{BackupDirectory}{storageInformation.FileName.ToLower()}.{fileType}"
                                              , FileMode.CreateNew
                                              , FileAccess.Write
                                             );
                storageInformation.MemoryStream.Position = 0;
                storageInformation.MemoryStream.WriteTo(MyBackupFile);
                MyBackupFile.Close();
            }
            catch (Exception)
            {
                storageInformation.ErrorMessage = "Storage.LocalStorage.SaveLocalFile - Unknown error while saving backup";
            }
        }
    }
    catch (Exception)
    {
        storageInformation.ErrorMessage = "Storage.LocalStorage.SaveLocalFile - Unknown error";
    }
}

Read

public static void ReadLocalFile(StorageInformation storageInformation)
{
    try
    {
        XElement storageXML = XElement.Parse(storageInformation.StorageDefinition);
        string PrimaryDirectory = $"{storageXML.GetElement("PrimaryDirectory", "")}{storageInformation.WorkspaceGUID}\\";

        DirectoryInfo MyPrimaryDir = new(PrimaryDirectory);

        if (MyPrimaryDir.Exists)
        {
            string fileType = storageInformation.IsFileCompressed ? "zip" : $"{storageInformation.FileType.ToLower()}";
            string MyFile = $"{PrimaryDirectory}{storageInformation.FileName.ToLower()}.{fileType}";
            byte[] bData = null;

            if (File.Exists(MyFile))
            {
                BinaryReader br = new(File.OpenRead(MyFile));
                bData = br.ReadBytes((int)br.BaseStream.Length);
                storageInformation.MemoryStream = new MemoryStream(bData, 0, bData.Length);
                br.Close();
            }
            else
            {
                storageInformation.ErrorMessage = $"File {MyFile} was not found";
            }
        }
        else
        {
            storageInformation.ErrorMessage = $"Directory {PrimaryDirectory} was not found";
        }
    }
    catch (Exception)
    {
        storageInformation.ErrorMessage = "Storage.LocalStorage.ReadLocalFile - Unknown error";
    }
}
Updated on January 2, 2024

Was this article helpful?

Related Articles

Need Support?
Can’t find the answer you’re looking for? Don’t worry we’re here to help!
Contact Support

Leave a Comment