/// <summary>
    /// If object exists returns the object else saves a plain object and returns it
    /// </summary>
    /// <param name="obj">empty placeholder object</param>
    /// <returns>Filesystem object</returns>
    public async Task <T> GetPersistantObject(T obj, string fileName)
    {
        List <string> files = new List <string>(_mvxFileStore.GetFilesIn(_mvxFileStore.NativePath("")));

        fileName = _mvxFileStore.NativePath(fileName);

        if (!files.Contains(fileName))
        {
            var objJson = Newtonsoft.Json.JsonConvert.SerializeObject(obj);

            objJson = bcEngine.Encrypt(objJson, currentkey_temp_dev);
            await _mvxFileStoreAsync.WriteFileAsync(fileName, objJson);
        }
        else
        {
            try
            {
                var temp = await _mvxFileStoreAsync.TryReadTextFileAsync(fileName);

                var str = bcEngine.Decrypt(temp.Result, currentkey_temp_dev);
                obj = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(str);
            }
            catch (Exception e) {
                var objJson = Newtonsoft.Json.JsonConvert.SerializeObject(obj);

                objJson = bcEngine.Encrypt(objJson, currentkey_temp_dev);
                await _mvxFileStoreAsync.WriteFileAsync(fileName, objJson);
            }
        }

        return(obj);
    }
예제 #2
0
        /// <summary>
        /// Store a string on a certain location
        /// </summary>
        /// <param name="folder">The name of the folder</param>
        /// <param name="fullName">The name of the file</param>
        /// <param name="contentAsString">The content that should be written to file</param>
        /// <returns></returns>
        public override async Task <string> StoreAsync(string folder, string fullName, string contentAsString)
        {
            var fullPath = FileChecks(folder, fullName);

            await _fileStoreAsync.WriteFileAsync(fullPath, contentAsString).ConfigureAwait(false);

            return(_fileStore.NativePath(fullPath));
        }
예제 #3
0
        public static async Task <string> StoreFileAsync(string folder, string fileFullName, string fileAsString)
        {
            var fullPath = FileChecks(folder, fileFullName);

            await FileStoreAsync.WriteFileAsync(fullPath, fileAsString).ConfigureAwait(false);

            return(FileStore.NativePath(fullPath));
        }
예제 #4
0
        /// <summary>
        /// Gets the filepath to a locally cached copy of an online image.
        /// </summary>
        /// <param name="imageUrl">Http/https URL of the required image resource.</param>
        /// <returns>Local filepath for the cached image, or null if the online resource was not found.</returns>
        public async Task <string> GetCachedIamgePathAsync(string imageUrl)
        {
            var localPath = imageUrl.Replace("https://", "");

            localPath = localPath.Replace("http://", "");
            localPath = localPath.Replace('/', '_');

            try
            {
                if (_fileStore.Exists(localPath))
                {
                    return(_fileStore.NativePath(localPath));
                }
            }
            catch (Exception)
            {
                // this is OK, an exception may be thrown here if the file wasn't found.
            }

            try
            {
                using (var httpClient = new HttpClient())
                {
                    var response = await httpClient.GetAsync(imageUrl);

                    if (response.IsSuccessStatusCode)
                    {
                        var bytes = await response.Content.ReadAsByteArrayAsync();

                        _fileStore.WriteFile(localPath, bytes);
                    }
                }
            }
            catch (Exception ex)
            {
                // TODO: handle http exceptions
            }

            return(_fileStore.NativePath(localPath));
        }
예제 #5
0
        private void checkForUnindexedFiles()
        {
            var           files     = storage.GetFilesIn("");
            List <string> filepaths = new List <string>();
            string        npath     = storage.NativePath("");

            npath += "/";
            //Standarization ---------GETFILESIN not reliable
            foreach (var f in files)
            {
                //Removing native path
                int    index     = f.IndexOf(npath);
                string cleanPath = (index < 0)
                ? f
                : f.Remove(index, npath.Length);
                filepaths.Add(cleanPath);
            }

            var cachedFiles = new Dictionary <string, Entry>();

            foreach (var entry in _indexByHttp)
            {
                cachedFiles[entry.Value.DownloadedPath] = entry.Value;
            }

            foreach (var file in filepaths)
            {
                bool endsWith = false;
                foreach (var pfilename in _persistentFiles)
                {
                    if (file.EndsWith(pfilename))
                    {
                        endsWith = true;
                    }
                }
                if (!cachedFiles.ContainsKey(file) && !endsWith)
                {
                    deleteFile(file);
                }
            }
        }