コード例 #1
0
ファイル: StorageFile.cs プロジェクト: jonfee/JF.CoreLibrary
        public void Create(StorageFileInfo file, Stream content)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            var storage = this.Storage;

            if (storage == null)
            {
                throw new InvalidOperationException("The Storage is null.");
            }

            //如果文件路径为空则为它设置一个有效的文件路径
            if (string.IsNullOrWhiteSpace(file.Path))
            {
                file.Path = this.GetFilePath(file);
            }

            storage.SetValue(GetFileKey(file.FileId), file.ToDictionary());

            var collection = storage.GetValue(GetFileCollectionKey(file.BucketId)) as ICollection <string>;

            if (collection != null)
            {
                collection.Add(file.FileId.ToString("n"));
            }
            else
            {
                storage.SetValue(GetFileCollectionKey(file.BucketId), new string[]
                {
                    file.FileId.ToString("n")
                });
            }

            if (content != null)
            {
                var path = JF.IO.Path.Parse(file.Path);

                //确认文件的所在目录是存在的,如果不存在则创建相应的目录
                FileSystem.Directory.Create(path.Scheme + ":" + path.DirectoryName);

                //创建或打开指定路径的文件流
                using (var stream = FileSystem.File.Open(file.Path, FileMode.Create, FileAccess.Write))
                {
                    //将文件内容写入到创建或打开的文件流中
                    content.CopyTo(stream);
                }
            }
        }
コード例 #2
0
        public static StorageFileInfo FromDictionary(IDictionary dictionary)
        {
            if (dictionary == null || dictionary.Count < 1)
            {
                return(null);
            }

            object bucketId, fileId, name, type, size, path, totalVisits, createdTime, modifiedTime, visitedTime;

            if (!dictionary.TryGetValue("BucketId", out bucketId))
            {
                return(null);
            }

            if (!dictionary.TryGetValue("FileId", out fileId))
            {
                return(null);
            }

            dictionary.TryGetValue("Name", out name);
            dictionary.TryGetValue("Type", out type);
            dictionary.TryGetValue("Size", out size);
            dictionary.TryGetValue("Path", out path);
            dictionary.TryGetValue("TotalVisits", out totalVisits);
            dictionary.TryGetValue("CreatedTime", out createdTime);
            dictionary.TryGetValue("ModifiedTime", out modifiedTime);
            dictionary.TryGetValue("VisitedTime", out visitedTime);

            var result = new StorageFileInfo(JF.Common.Convert.ConvertValue <int>(bucketId), JF.Common.Convert.ConvertValue <Guid>(fileId))
            {
                Name = JF.Common.Convert.ConvertValue <string>(name), Type = JF.Common.Convert.ConvertValue <string>(type), Size = JF.Common.Convert.ConvertValue <long>(size), Path = JF.Common.Convert.ConvertValue <string>(path), TotalVisits = JF.Common.Convert.ConvertValue <int>(totalVisits), CreatedTime = JF.Common.Convert.ConvertValue <DateTime>(createdTime), ModifiedTime = JF.Common.Convert.ConvertValue <DateTime?>(modifiedTime), VisitedTime = JF.Common.Convert.ConvertValue <DateTime?>(visitedTime),
            };

            foreach (var key in dictionary.Keys)
            {
                if (key == null)
                {
                    continue;
                }

                if (key.ToString().StartsWith(EXTENDEDPROPERTIESPREFIX))
                {
                    result.ExtendedProperties[key.ToString().Substring(EXTENDEDPROPERTIESPREFIX.Length)] = dictionary[key];
                }
            }

            return(result);
        }
コード例 #3
0
ファイル: StorageFile.cs プロジェクト: jonfee/JF.CoreLibrary
        private string GetFilePath(StorageFileInfo fileInfo)
        {
            var timestamp  = fileInfo.CreatedTime;
            var parameters = JF.Collections.DictionaryExtension.ToDictionary <string, string>(Environment.GetEnvironmentVariables());

            parameters.Add("date", timestamp.ToString("yyyyMMdd"));
            parameters.Add("time", timestamp.ToString("HHmmss"));
            parameters.Add("year", timestamp.Year.ToString("0000"));
            parameters.Add("month", timestamp.Month.ToString("00"));
            parameters.Add("day", timestamp.Day.ToString("00"));

            var resolvedPath = this.ResolveTextWithParameters(this.GetBucketPath(fileInfo.BucketId), parameters);

            //返回当前文件的完整虚拟路径
            return(JF.IO.Path.Combine(resolvedPath, string.Format("[{0}]{1:n}{2}", fileInfo.BucketId, fileInfo.FileId, System.IO.Path.GetExtension(fileInfo.Name).ToLowerInvariant())));
        }
コード例 #4
0
ファイル: StorageFile.cs プロジェクト: jonfee/JF.CoreLibrary
        public StorageFileInfo GetInfo(Guid fileId)
        {
            var storage = this.Storage;

            if (storage == null)
            {
                throw new InvalidOperationException("The Storage is null.");
            }

            var dictionary = storage.GetValue(GetFileKey(fileId)) as IDictionary;

            if (dictionary != null)
            {
                return(StorageFileInfo.FromDictionary(dictionary));
            }

            return(null);
        }
コード例 #5
0
ファイル: StorageFile.cs プロジェクト: jonfee/JF.CoreLibrary
        public Stream Open(Guid fileId, out StorageFileInfo info)
        {
            var storage = this.Storage;

            if (storage == null)
            {
                throw new InvalidOperationException("The Storage is null.");
            }

            info = null;

            var dictionary = storage.GetValue(GetFileKey(fileId)) as IDictionary;

            if (dictionary == null || dictionary.Count < 1)
            {
                return(null);
            }

            info = StorageFileInfo.FromDictionary(dictionary);

            if (dictionary is JF.Common.IAccumulator)
            {
                ((JF.Common.IAccumulator)dictionary).Increment("TotalVisits");
            }
            else
            {
                long totalVisits = 0;
                dictionary.TryGetValue <long>("TotalVisits", out totalVisits);
                dictionary["TotalVisits"] = totalVisits + 1;
            }

            dictionary["VisitedTime"] = DateTime.Now;

            if (string.IsNullOrWhiteSpace(info.Path))
            {
                return(null);
            }

            return(FileSystem.File.Open(info.Path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read));
        }
コード例 #6
0
ファイル: StorageFile.cs プロジェクト: jonfee/JF.CoreLibrary
        public Guid?Copy(Guid fileId, int bucketId)
        {
            var storage = this.Storage;

            if (storage == null)
            {
                throw new InvalidOperationException("The Storage is null.");
            }

            var info = this.GetInfo(fileId);

            if (info == null)
            {
                return(null);
            }

            if (info.BucketId == bucketId)
            {
                return(fileId);
            }

            var newInfo = new StorageFileInfo(bucketId, Guid.NewGuid())
            {
                Name = info.Name, Type = info.Type, Size = info.Size, Path = info.Path,
            };

            if (info.HasExtendedProperties)
            {
                foreach (var entry in info.ExtendedProperties)
                {
                    newInfo.ExtendedProperties.Add(entry.Key, entry.Value);
                }
            }

            this.Create(newInfo, null);

            return(newInfo.FileId);
        }