/// <summary>
        /// Creates a file under the given directory.
        /// </summary>
        /// <param name="baseDirectory">The base Directory to create file under it.</param>
        /// <param name="fileName">The file name.</param>
        /// <returns>The file created, or null if file cannot be created.</returns>
        public FileData CreateFile(string fileName, DirectoryData baseDirectory)
        {
            FileData file     = null;
            string   path     = Path.Combine(baseDirectory.FullName, fileName);
            string   rootPath = GetDirectoryRoot().FullName;

            if (baseDirectory != null)
            {
                if (!path.StartsWith(rootPath))
                {
                    path = Path.Combine(rootPath, path);                       // concats root path to the given base path.
                }
            }
            else
            {
                path = rootPath;                 // creates file under root path.
            }

            if (!File.Exists(path))
            {
                try {
                    FileStream fs = File.Create(path);
                    file = new FileData(path, fs.Length);
                    fs.Close();
                } catch (Exception e) {
                    SystemLogger.Log(SystemLogger.Module.CORE, "Error creating file [" + path + "]", e);
                }
            }

            return(file);
        }
        /// <summary>
        /// Creates a directory under the given base directory.
        /// </summary>
        /// <param name="baseDirectory">>The base Directory to create directory under it.</param>
        /// <param name="directoryName">The directory name. If null, directory is created under root path.</param>
        /// <returns>The directory created, or null if file cannot be created.</returns>
        public DirectoryData CreateDirectory(string directoryName, DirectoryData baseDirectory)
        {
            DirectoryData directory = null;
            string        path      = Path.Combine(baseDirectory.FullName, directoryName);
            string        rootPath  = GetDirectoryRoot().FullName;

            if (baseDirectory != null)
            {
                if (!path.StartsWith(rootPath))
                {
                    path = Path.Combine(rootPath, path);                       // concats root path to the given base path.
                }
            }
            else
            {
                path = rootPath;                           // creates directory under root path.
            }
            if (!ExistsDirectory(new DirectoryData(path))) // creates directory if it does not already exist.
            {
                try {
                    directory = new DirectoryData(Directory.CreateDirectory(path).FullName);
                } catch (Exception e) {
                    SystemLogger.Log(SystemLogger.Module.CORE, "Error creating directory [" + path + "]", e);
                }
            }

            return(directory);
        }
        /// <summary>
        /// Deletes a directory (deleting first all its children).
        /// </summary>
        /// <param name="directory">The directory to be deleted.</param>
        /// <returns>True on successful deletion.</returns>
        public bool DeleteDirectory(DirectoryData directory)
        {
            bool deleted = false;

            if (directory != null)
            {
                string roothPath = GetDirectoryRoot().FullName;
                try {
                    if (!directory.FullName.StartsWith(roothPath))
                    {
                        directory.FullName = Path.Combine(roothPath, directory.FullName);
                    }
                    //TODO Review
                    if (!CheckSecurePath(directory.FullName))
                    {
                        return(false);
                    }
                    Directory.Delete(directory.FullName, true);                      // recursively delete contents
                    deleted = true;
                } catch (Exception e) {
                    SystemLogger.Log(SystemLogger.Module.CORE, "Error deleting directory [" + directory.FullName + "]", e);
                }
            }

            return(deleted);
        }
 public override async Task<bool> DeleteDirectory(DirectoryData directory)
 {
     var folderToDelete = await GetStorageItem(GetDirectoryPath(directory), true);
     if (folderToDelete == null || folderToDelete.Path.Equals(_LocalDocumentsFolder.Path) || folderToDelete.Path.Equals(ApplicationData.Current.LocalFolder.Path)) return false;
     await folderToDelete.DeleteAsync(StorageDeleteOption.PermanentDelete);
     return true;
 }
        /// <summary>
        /// Copies the given file on "fromPath" to the "toPath".
        /// </summary>
        /// <param name="fromPath">Relative path under configured application "resources" directory.
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="toPath">Relative path under configured application "documents" directory. See GetDirectoryRoot().
        /// A <see cref="System.String"/>
        /// </param>
        /// <returns>
        /// A <see cref="System.Boolean"/>
        /// </returns>
        public bool CopyFromResources(string fromPath, string toPath)
        {
            try {
                DirectoryData resourcesDir = GetDirectoryResources();
                string        fromFilePath = Path.Combine(resourcesDir.FullName, fromPath);
                FileData      sourceFile   = new FileData(fromFilePath);

                if (ExistsFile(sourceFile))
                {
                    DirectoryData rootDir    = GetDirectoryRoot();
                    string        toFilePath = Path.Combine(rootDir.FullName, toPath);

                    try {
                        File.Copy(fromFilePath, toFilePath);
                    } catch (Exception ex) {
                        SystemLogger.Log(SystemLogger.Module.CORE, "Error copying from file [" + fromFilePath + "] to file [" + toFilePath + "]", ex);
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    SystemLogger.Log(SystemLogger.Module.CORE, "Error copying from file [" + fromFilePath + "]. File does not exists.");
                }
            } catch (Exception) {
                SystemLogger.Log(SystemLogger.Module.CORE, "Error copying from file [" + fromPath + "]. Unhandled exception.");
            }

            return(false);
        }
		/// <summary>
		/// Lists the installed modules.
		/// </summary>
		/// <returns>The installed modules.</returns>
		public override Module[] ListInstalledModules ()
		{
			List<Module> list = new List<Module> ();

			string ModulesPath = this.GetModulesRootPath();
			SystemLogger.Log(SystemLogger.Module.PLATFORM, "Listing installed modules under path: " + ModulesPath);

			DirectoryData modulesDirectory = new DirectoryData(ModulesPath);

			if(this.GetFileSystemService().ExistsDirectory(modulesDirectory)) {
				DirectoryData[] apps = this.GetFileSystemService().ListDirectories(modulesDirectory);
				foreach(DirectoryData app in apps) {
					SystemLogger.Log(SystemLogger.Module.PLATFORM, "directory: " + app.FullName);
					DirectoryData[] versions = this.GetFileSystemService().ListDirectories(app);

					foreach(DirectoryData version in versions) {
						SystemLogger.Log(SystemLogger.Module.PLATFORM, "version: " + version.FullName);
						Module module = new Module();
						module.Id = Path.GetFileName(app.FullName);
						module.Version = this.ParseModuleVersion(Path.GetFileName(version.FullName));
						list.Add(module);
					}
				}
			} else {
				SystemLogger.Log(SystemLogger.Module.PLATFORM, "Modules directory does not exists: " + ModulesPath);
			}

			return list.ToArray();
		}
        /// <summary>
        /// Checks if given directory exists.
        /// </summary>
        /// <param name="directory">The directory to be checked.</param>
        /// <returns>True if directory exists.</returns>
        public bool ExistsDirectory(DirectoryData directory)
        {
            if (directory == null)
            {
                return(false);
            }
            string roothPath = GetDirectoryRoot().FullName;

            if (!directory.FullName.StartsWith(roothPath))
            {
                directory.FullName = Path.Combine(roothPath, directory.FullName);
            }
            return(Directory.Exists(directory.FullName));
        }
		public override void GenerateQRCode(MediaQRContent content)
		{
			SystemLogger.Log (SystemLogger.Module.PLATFORM, "1");
			try{
				MediaMetadata mediaData = new MediaMetadata();
				SystemLogger.Log (SystemLogger.Module.PLATFORM, "2");

				int size = content.Size;
				if (size == 0) {
					size = 256;
				}
				var writer = new ZXing.BarcodeWriter
				{ 
					Format = BarcodeFormat.QR_CODE, 
					Options = new EncodingOptions { Height = size, Width = size } 
				};
				//var img = writer.Write(content.Text);
				SystemLogger.Log (SystemLogger.Module.PLATFORM, "3");

				var uuid = Guid.NewGuid ();
				string s = uuid.ToString ();
				String filename = "QR_" + s;
				NSError err;
				DirectoryData dest = new DirectoryData(DEFAULT_ROOT_PATH);
				string path = Path.Combine(dest.FullName, filename+".png");
				SystemLogger.Log (SystemLogger.Module.PLATFORM, "4");
				content = encodeQRCodeContents(content);
				using(UIImage img = writer.Write(content.Text)) {
					
					using (var data = img.AsPNG ()) {
						data.Save (path, true, out err);
					}
				}
				SystemLogger.Log (SystemLogger.Module.PLATFORM, "5");

				mediaData.ReferenceUrl = filename+".png";
				mediaData.Title = filename;

				SystemLogger.Log (SystemLogger.Module.PLATFORM, "6");

				UIApplication.SharedApplication.InvokeOnMainThread (delegate {
					UIViewController viewController = UIApplication.SharedApplication.KeyWindow.RootViewController;
					FireUnityJavascriptEvent(viewController, "Appverse.Scanner.onGeneratedQR", mediaData);

				});
			}catch(Exception ex)
			{
				SystemLogger.Log (SystemLogger.Module.PLATFORM, "GenerateQRCode - exception: " + ex.Message);
			}
		}
        /// <summary>
        /// List all files under given directory.
        /// </summary>
        /// <param name="directory">The directory to list files under it.</param>
        /// <returns>List of files.</returns>
        public FileData[] ListFiles(DirectoryData directory)
        {
            if (directory == null)
            {
                return(GetDirectoryRoot().GetFiles());
            }

            string roothPath = GetDirectoryRoot().FullName;

            if (!directory.FullName.StartsWith(roothPath))
            {
                directory.FullName = Path.Combine(roothPath, directory.FullName);
            }
            return(directory.GetFiles());
        }
Exemple #10
0
        public DirectoryData[] GetDirectories()
        {
            List<DirectoryData> list = new List<DirectoryData> ();
            try {
                DirectoryInfo directory = new DirectoryInfo (this.FullName);
                DirectoryInfo[] directories = directory.GetDirectories ();
                foreach (DirectoryInfo dir in directories) {
                    DirectoryData dirData = new DirectoryData (dir.FullName);
                    list.Add (dirData);
                }
            } catch (Exception e) {
                SystemLogger.Log (SystemLogger.Module .CORE, "Error getting files for directory [" + this.FullName + "]", e);
            }

            return list.ToArray ();
        }
        /// <summary>
        /// Stores the file, under the given directory, with the given contents.
        /// </summary>
        /// <returns>
        /// The file. The file contents are overwritten if it already exists.
        /// </returns>
        /// <param name='directoryPath'>
        /// Directory path. The directory is created if it does not exist.
        /// </param>
        /// <param name='fileName'>
        /// File name.
        /// </param>
        /// <param name='fileData'>
        /// File data.
        /// </param>
        public string StoreFile(string directoryPath, string fileName, byte[] fileData)
        {
            // Create directory if it does not exists
            DirectoryData assetsDirectory = this.CreateDirectory(directoryPath);

            if (assetsDirectory == null)
            {
                // Directory already exists
                assetsDirectory = new DirectoryData(Path.Combine(this.GetDirectoryRoot().FullName, directoryPath));
            }
            //TODO Review
            if (!CheckSecurePath(assetsDirectory.FullName))
            {
                return(null);
            }

            SystemLogger.Log(SystemLogger.Module.CORE, "Directory created: " + assetsDirectory.FullName);

            // Create file on the directory
            FileData storedFile = this.CreateFile(fileName, assetsDirectory);

            if (storedFile == null)
            {
                // File already created (would be overwritten with the WriteFile call)
                storedFile = new FileData(Path.Combine(assetsDirectory.FullName, fileName), 0);
            }
            //TODO Review
            if (!CheckSecurePath(storedFile.FullName))
            {
                return(null);
            }
            // Write contents to file
            bool success = this.WriteFile(storedFile, fileData, false);

            if (success)
            {
                SystemLogger.Log(SystemLogger.Module.CORE, "File sucessfully stored locally: " + storedFile.FullName);
            }
            else
            {
                SystemLogger.Log(SystemLogger.Module.CORE, "File could not be stored locally to path: " + storedFile.FullName);
            }

            return(Path.Combine(directoryPath, fileName));
        }
        public DirectoryData[] GetDirectories()
        {
            List <DirectoryData> list = new List <DirectoryData> ();

            try {
                DirectoryInfo   directory   = new DirectoryInfo(this.FullName);
                DirectoryInfo[] directories = directory.GetDirectories();
                foreach (DirectoryInfo dir in directories)
                {
                    DirectoryData dirData = new DirectoryData(dir.FullName);
                    list.Add(dirData);
                }
            } catch (Exception e) {
                SystemLogger.Log(SystemLogger.Module.CORE, "Error getting files for directory [" + this.FullName + "]", e);
            }


            return(list.ToArray());
        }
        /// <summary>
        /// List all directories under given directory.
        /// </summary>
        /// <param name="directory">The directry to list files under it.</param>
        /// <returns>List of directories.</returns>
        public DirectoryData[] ListDirectories(DirectoryData directory)
        {
            if (directory == null)
            {
                return(ListDirectories());
            }

            string roothPath = GetDirectoryRoot().FullName;

            if (!directory.FullName.StartsWith(roothPath))
            {
                directory.FullName = Path.Combine(roothPath, directory.FullName);
            }
            //TODO Review
            if (!CheckSecurePath(directory.FullName))
            {
                return(null);
            }
            return(directory.GetDirectories());
        }
 private async Task<StorageFolder> CreateFolderTree(DirectoryData baseDirectory)
 {
     if (baseDirectory == null || String.IsNullOrWhiteSpace(baseDirectory.FullName)) return _LocalDocumentsFolder;
     var returnFolder = _LocalDocumentsFolder;
     if (Path.HasExtension(baseDirectory.FullName))
         baseDirectory.FullName = Path.GetDirectoryName(baseDirectory.FullName);
     foreach (var directoryName in baseDirectory.FullName.Split(new[] { @"/", @"\" },
         StringSplitOptions.RemoveEmptyEntries))
     {
         returnFolder = await returnFolder.CreateFolderAsync(directoryName, CreationCollisionOption.OpenIfExists);
     }
     return returnFolder;
 }
 private string GetDirectoryPath(DirectoryData folderObject)
 {
     return folderObject == null ? _LocalDocumentsFolder.Path : GetItemPath(folderObject.FullName);
 }
 public override async Task<bool> ExistsDirectory(DirectoryData directory)
 {
     var targetDirectory = await GetStorageItem(GetDirectoryPath(directory), true);
     return targetDirectory != null;
 }
 public abstract Task<FileData[]> ListFiles(DirectoryData directory);
 public abstract Task<DirectoryData[]> ListDirectories(DirectoryData directory);
 public abstract Task <bool> ExistsDirectory(DirectoryData directory);
 public abstract Task <bool> DeleteDirectory(DirectoryData directory);
 public abstract Task<FileData> CreateFile(string fileName, DirectoryData baseDirectory);
 public abstract Task <DirectoryData> CreateDirectory(string directoryName, DirectoryData baseDirectory);
        /// <summary>
        /// Stores the file, under the given directory, with the given contents.
        /// </summary>
        /// <returns>
        /// The file. The file contents are overwritten if it already exists.
        /// </returns>
        /// <param name='directoryPath'>
        /// Directory path. The directory is created if it does not exist.
        /// </param>
        /// <param name='fileName'>
        /// File name.
        /// </param>
        /// <param name='fileData'>
        /// File data.
        /// </param>
        public string StoreFile(string directoryPath, string fileName, byte[] fileData)
        {
            // Create directory if it does not exists
            DirectoryData assetsDirectory = this.CreateDirectory (directoryPath);
            if (assetsDirectory == null) {
                // Directory already exists
                assetsDirectory = new DirectoryData (Path.Combine (this.GetDirectoryRoot ().FullName, directoryPath));
            }

            SystemLogger.Log (SystemLogger.Module.CORE, "Directory created: " + assetsDirectory.FullName);

            // Create file on the directory
            FileData storedFile = this.CreateFile (fileName, assetsDirectory);
            if (storedFile == null) {
                // File already created (would be overwritten with the WriteFile call)
                storedFile = new FileData (Path.Combine (assetsDirectory.FullName, fileName), 0);
            }

            // Write contents to file
            bool success = this.WriteFile (storedFile, fileData, false);
            if (success) {
                SystemLogger.Log (SystemLogger.Module.CORE, "File sucessfully stored locally: " + storedFile.FullName);
            } else {
                SystemLogger.Log (SystemLogger.Module.CORE, "File could not be stored locally to path: " + storedFile.FullName);
            }

            return Path.Combine (directoryPath, fileName);
        }
 /// <summary>
 /// Checks if given directory exists.
 /// </summary>
 /// <param name="directory">The directory to be checked.</param>
 /// <returns>True if directory exists.</returns>
 public bool ExistsDirectory(DirectoryData directory)
 {
     if (directory == null) {
         return false;
     }
     string roothPath = GetDirectoryRoot ().FullName;
     if (!directory.FullName.StartsWith (roothPath)) {
         directory.FullName = Path.Combine (roothPath, directory.FullName);
     }
     return Directory.Exists (directory.FullName);
 }
        /// <summary>
        /// Creates a file under the given directory.
        /// </summary>
        /// <param name="baseDirectory">The base Directory to create file under it.</param>
        /// <param name="fileName">The file name.</param>
        /// <returns>The file created, or null if file cannot be created.</returns>
        public FileData CreateFile(string fileName, DirectoryData baseDirectory)
        {
            FileData file = null;
            string path = Path.Combine (baseDirectory.FullName, fileName);
            string rootPath = GetDirectoryRoot ().FullName;

            if (baseDirectory != null) {
                if (!path.StartsWith (rootPath)) {
                    path = Path.Combine (rootPath, path);  // concats root path to the given base path.
                }
            } else {
                path = rootPath; // creates file under root path.
            }

            if (!File.Exists (path)) {
                try {
                    FileStream fs = File.Create (path);
                    file = new FileData (path, fs.Length);
                    fs.Close ();
                } catch (Exception e) {
                    SystemLogger.Log (SystemLogger.Module .CORE, "Error creating file [" + path + "]", e);
                }
            }

            return file;
        }
 public abstract Task <FileData[]> ListFiles(DirectoryData directory);
 public abstract Task <DirectoryData[]> ListDirectories(DirectoryData directory);
 public override async Task<FileData> CreateFile(string fileName, DirectoryData baseDirectory)
 {
     var folder = await CreateFolderTree(baseDirectory);
     var createdFile = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
     return new FileData(createdFile.Path.Replace(_LocalDocumentsFolder.Path, String.Empty));
 }
 public override async Task<DirectoryData> CreateDirectory(string directoryName, DirectoryData baseDirectory)
 {
     var folder = await CreateFolderTree(baseDirectory);
     var createdFolder = await folder.CreateFolderAsync(directoryName, CreationCollisionOption.OpenIfExists);
     return new DirectoryData(createdFolder.Path.Replace(_LocalDocumentsFolder.Path, String.Empty));
 }
 public abstract Task<bool> DeleteDirectory(DirectoryData directory);
 public override async Task<DirectoryData[]> ListDirectories(DirectoryData directory)
 {
     var selectedFolder = await GetStorageItem(GetDirectoryPath(directory), true) as StorageFolder;
     return selectedFolder == null ? null : (await selectedFolder.GetFoldersAsync()).Select(folder => new DirectoryData(folder.Path.Remove(0, _LocalDocumentsFolder.Path.Length))).ToArray();
 }
 public abstract Task<bool> ExistsDirectory(DirectoryData directory);
        /// <summary>
        /// Deletes a directory (deleting first all its children).
        /// </summary>
        /// <param name="directory">The directory to be deleted.</param>
        /// <returns>True on successful deletion.</returns>
        public bool DeleteDirectory(DirectoryData directory)
        {
            bool deleted = false;
            if (directory != null) {
                string roothPath = GetDirectoryRoot ().FullName;
                try {
                    if (!directory.FullName.StartsWith (roothPath)) {
                        directory.FullName = Path.Combine (roothPath, directory.FullName);
                    }
                    Directory.Delete (directory.FullName, true); // recursively delete contents
                    deleted = true;
                } catch (Exception e) {
                    SystemLogger.Log (SystemLogger.Module .CORE, "Error deleting directory [" + directory.FullName + "]", e);
                }
            }

            return deleted;
        }
        /// <summary>
        /// List all files under given directory.
        /// </summary>
        /// <param name="directory">The directory to list files under it.</param>
        /// <returns>List of files.</returns>
        public FileData[] ListFiles(DirectoryData directory)
        {
            if (directory == null)
            {
                return GetDirectoryRoot().GetFiles();
            }

            string roothPath = GetDirectoryRoot().FullName;
            if (!directory.FullName.StartsWith(roothPath))
            {
                directory.FullName = Path.Combine(roothPath, directory.FullName);
            }
            //TODO Review
            if (!CheckSecurePath(directory.FullName))
                return null;
            return directory.GetFiles();
        }
        /// <summary>
        /// List all files under given directory.
        /// </summary>
        /// <param name="directory">The directory to list files under it.</param>
        /// <returns>List of files.</returns>
        public FileData[] ListFiles(DirectoryData directory)
        {
            if (directory == null) {
                return GetDirectoryRoot ().GetFiles ();
            }

            string roothPath = GetDirectoryRoot ().FullName;
            if (!directory.FullName.StartsWith (roothPath)) {
                directory.FullName = Path.Combine (roothPath, directory.FullName);
            }
            return directory.GetFiles ();
        }
 public abstract Task<DirectoryData> CreateDirectory(string directoryName, DirectoryData baseDirectory);
        /// <summary>
        /// Creates a directory under the given base directory.
        /// </summary>
        /// <param name="baseDirectory">>The base Directory to create directory under it.</param>
        /// <param name="directoryName">The directory name. If null, directory is created under root path.</param>
        /// <returns>The directory created, or null if file cannot be created.</returns>
        public DirectoryData CreateDirectory(string directoryName, DirectoryData baseDirectory)
        {
            DirectoryData directory = null;
            string path = Path.Combine (baseDirectory.FullName, directoryName);
            string rootPath = GetDirectoryRoot ().FullName;
            if (baseDirectory != null) {
                if (!path.StartsWith (rootPath)) {
                    path = Path.Combine (rootPath, path);  // concats root path to the given base path.
                }
            } else {
                path = rootPath; // creates directory under root path.
            }
            if (!ExistsDirectory (new DirectoryData (path))) {  // creates directory if it does not already exist.
                try {
                    directory = new DirectoryData (Directory.CreateDirectory (path).FullName);
                } catch (Exception e) {
                    SystemLogger.Log (SystemLogger.Module .CORE, "Error creating directory [" + path + "]", e);
                }
            }

            return directory;
        }
 public abstract Task <FileData> CreateFile(string fileName, DirectoryData baseDirectory);