Exemplo n.º 1
0
            private Zongsoft.IO.FileInfo GenerateInfo(string path, IDictionary <string, object> properties)
            {
                if (properties == null)
                {
                    return(null);
                }

                DateTimeOffset createdTimeOffset, modifiedTimeOffset;
                DateTime?      createdTime = null, modifiedTime = null;
                int            size = 0;

                byte[] checksum = null;
                object value;

                if (properties.TryGetValue(StorageHeaders.ZFS_CREATEDTIME_PROPERTY, out value))
                {
                    if (Zongsoft.Common.Convert.TryConvertValue(value, out createdTimeOffset))
                    {
                        createdTime = createdTimeOffset.LocalDateTime;
                    }
                }

                if (properties.TryGetValue(StorageHeaders.HTTP_LAST_MODIFIED_PROPERTY, out value))
                {
                    if (Zongsoft.Common.Convert.TryConvertValue(value, out modifiedTimeOffset))
                    {
                        modifiedTime = modifiedTimeOffset.LocalDateTime;
                    }
                }

                if (properties.TryGetValue(StorageHeaders.HTTP_CONTENT_LENGTH_PROPERTY, out value))
                {
                    Zongsoft.Common.Convert.TryConvertValue(value, out size);
                }

                if (properties.TryGetValue(StorageHeaders.HTTP_ETAG_PROPERTY, out value) && value != null)
                {
                    checksum = Zongsoft.Common.Convert.FromHexString(value.ToString().Trim('"'), '-');
                }

                var info = new Zongsoft.IO.FileInfo(path, size, checksum, createdTime, modifiedTime, _fileSystem.GetUrl(path));

                foreach (var property in properties)
                {
                    info.Properties[property.Key] = property.Value;
                }

                return(info);
            }
Exemplo n.º 2
0
            public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
            {
                if (parent == null)
                {
                    throw new ArgumentNullException(nameof(parent));
                }
                if (headers == null)
                {
                    throw new ArgumentNullException(nameof(headers));
                }

                //判断当前内容项是否为普通表单域,如果是则返回一个内存流
                if (headers.ContentDisposition == null || string.IsNullOrEmpty(headers.ContentDisposition.FileName))
                {
                    //在表单数据标记列表中按顺序将当前内容标记为普通表单域
                    _isFormData.Add(true);

                    //返回一个新的内存流
                    return(new MemoryStream());
                }

                //获取当前内容项的内容类型
                var contentType = headers.ContentType == null ? string.Empty : headers.ContentType.MediaType;

                if (string.IsNullOrWhiteSpace(contentType))
                {
                    contentType = System.Web.MimeMapping.GetMimeMapping(headers.ContentDisposition.FileName);
                }

                string fileName = null;

                //设置文件名的初始值
                if (_filePath[_filePath.Length - 1] != '/')
                {
                    var index = _filePath.LastIndexOf('/');

                    if (index > 0 && index < _filePath.Length - 1)
                    {
                        fileName = _filePath.Substring(index + 1);
                    }
                }

                var dispositionName = UnquoteToken(headers.ContentDisposition.Name);
                var extensionName   = System.IO.Path.GetExtension(UnquoteToken(headers.ContentDisposition.FileName));

                if (!string.IsNullOrWhiteSpace(dispositionName))
                {
                    //获取请求头中显式指定的文件名(注意:该文件名支持模板格式)
                    if (_headers.TryGetValue(dispositionName + ".name", out var value))
                    {
                        fileName = Zongsoft.Text.TemplateEvaluatorManager.Default.Evaluate <string>(value, null).ToLowerInvariant() + extensionName;
                    }
                }

                //定义文件写入的模式
                bool overwrite = false;

                //定义文件的扩展名是否自动添加
                bool extensionAppend = true;

                //执行写入前的回调方法
                if (_onWriting != null)
                {
                    //创建回调参数
                    var args = new WritingEventArgs(_filePath, fileName, Interlocked.Increment(ref _index));

                    //执行写入前的回调
                    _onWriting(args);

                    if (args.Cancel)
                    {
                        return(null);
                    }

                    //获取文件写入参数
                    fileName        = args.FileName;
                    overwrite       = args.Overwrite;
                    extensionAppend = args.ExtensionAppend;
                }

                //如果文件名为空,则生成一个以“时间戳-随机数.ext”的默认文件名
                if (string.IsNullOrWhiteSpace(fileName))
                {
                    fileName = string.Format("X{0}-{1}{2}", ((long)(DateTime.UtcNow - EPOCH).TotalSeconds).ToString(), Zongsoft.Common.Randomizer.GenerateString(), extensionAppend ? extensionName : string.Empty);
                }
                else if (extensionAppend && !fileName.EndsWith(extensionName))
                {
                    fileName += extensionName;
                }

                //生成文件的完整路径
                var filePath = Zongsoft.IO.Path.Combine(_directory, fileName);

                //生成文件信息的描述实体
                var fileInfo = new Zongsoft.IO.FileInfo(filePath, (headers.ContentDisposition.Size.HasValue ? headers.ContentDisposition.Size.Value : -1), DateTime.Now, null, FileSystem.GetUrl(filePath))
                {
                    Type = contentType,
                };

                //将上传的文件项的键名加入到文件描述实体的扩展属性中
                if (!string.IsNullOrWhiteSpace(dispositionName))
                {
                    fileInfo.Properties.Add(EXTENDED_PROPERTY_DISPOSITIONNAME, dispositionName);
                }

                //将上传的原始文件名加入到文件描述实体的扩展属性中
                fileInfo.Properties.Add(EXTENDED_PROPERTY_FILENAME, Uri.UnescapeDataString(UnquoteToken(headers.ContentDisposition.FileName)));

                if (_headers != null && _headers.Count > 0 && !string.IsNullOrWhiteSpace(dispositionName))
                {
                    //从全局头里面查找当前上传文件的自定义属性
                    foreach (var header in _headers)
                    {
                        if (header.Key.Length > dispositionName.Length + 1 && header.Key.StartsWith(dispositionName + "-", StringComparison.OrdinalIgnoreCase))
                        {
                            fileInfo.Properties.Add(header.Key.Substring(dispositionName.Length + 1), header.Value);
                        }
                    }
                }

                //将文件信息对象加入到集合中
                _fileData.Add(fileInfo);

                //在表单数据标记列表中按顺序将当前内容标记为非普通表单域(即二进制文件域)
                _isFormData.Add(false);

                try
                {
                    //调用文件系统根据完整文件路径去创建一个新建文件流
                    var stream = FileSystem.File.Open(filePath, (overwrite ? FileMode.Create : FileMode.CreateNew), FileAccess.Write, (fileInfo.HasProperties ? fileInfo.Properties : null));

                    fileInfo.Properties.Add(EXTENDED_PROPERTY_FILESTREAM, stream);

                    return(stream);
                }
                catch
                {
                    if (fileInfo != null)
                    {
                        _fileData.Remove(fileInfo);
                    }

                    throw;
                }
            }
			private Zongsoft.IO.FileInfo GenerateInfo(string path, IDictionary<string, string> properties)
			{
				if(properties == null)
					return null;

				DateTimeOffset createdTimeOffset, modifiedTimeOffset;
				DateTime? createdTime = null, modifiedTime = null;
				int size = 0;
				byte[] checksum = null;
				string text;

				if(properties.TryGetValue(StorageHeaders.ZFS_CREATEDTIME_PROPERTY, out text))
				{
					if(Zongsoft.Common.Convert.TryConvertValue(text, out createdTimeOffset))
						createdTime = createdTimeOffset.LocalDateTime;
				}

				if(properties.TryGetValue(StorageHeaders.HTTP_LAST_MODIFIED_PROPERTY, out text))
				{
					if(Zongsoft.Common.Convert.TryConvertValue(text, out modifiedTimeOffset))
						modifiedTime = modifiedTimeOffset.LocalDateTime;
				}

				if(properties.TryGetValue(StorageHeaders.HTTP_CONTENT_LENGTH_PROPERTY, out text))
					Zongsoft.Common.Convert.TryConvertValue(text, out size);

				if(properties.TryGetValue(StorageHeaders.HTTP_ETAG_PROPERTY, out text))
					checksum = Zongsoft.Common.Convert.FromHexString(text.Trim('"'), '-');

				var info = new Zongsoft.IO.FileInfo(path, size, checksum, createdTime, modifiedTime, _fileSystem.GetUrl(path));

				foreach(var property in properties)
				{
					info.Properties[property.Key] = property.Value;
				}

				return info;
			}
Exemplo n.º 4
0
			public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
			{
				if(parent == null)
					throw new ArgumentNullException("parent");

				if(headers == null)
					throw new ArgumentNullException("headers");

				if(headers.ContentDisposition == null || string.IsNullOrEmpty(headers.ContentDisposition.FileName))
				{
					_isFormData.Add(true);
					return new MemoryStream();
				}

				var contentType = headers.ContentType == null ? string.Empty : headers.ContentType.MediaType;

				if(string.IsNullOrWhiteSpace(contentType))
					contentType = System.Web.MimeMapping.GetMimeMapping(headers.ContentDisposition.FileName);

				_isFormData.Add(false);

				string fileName = null;
				var dispositionName = this.UnquoteToken(headers.ContentDisposition.Name);

				if(!string.IsNullOrWhiteSpace(dispositionName))
				{
					if(_headers.TryGetValue(dispositionName + ".name", out fileName))
						fileName = Zongsoft.Text.TemplateEvaluatorManager.Default.Evaluate<string>(fileName, null).ToLowerInvariant() + System.IO.Path.GetExtension(headers.ContentDisposition.FileName.Trim('"'));
				}

				if(string.IsNullOrWhiteSpace(fileName))
					fileName = string.Format("{0:yyyyMMdd-HHmmss}-{1}{2}", DateTime.Now, (uint)Zongsoft.Common.RandomGenerator.GenerateInt32(), System.IO.Path.GetExtension(headers.ContentDisposition.FileName.Trim('"')));

				var filePath = Zongsoft.IO.Path.Combine(_directoryPath, fileName);

				Zongsoft.IO.FileInfo fileInfo = new Zongsoft.IO.FileInfo(filePath, (headers.ContentDisposition.Size.HasValue ? headers.ContentDisposition.Size.Value : -1), DateTime.Now, null, FileSystem.GetUrl(filePath));
				fileInfo.Properties.Add("FileName", Uri.UnescapeDataString(headers.ContentDisposition.FileName.Trim('"')));

				if(_headers != null && _headers.Count > 0 && !string.IsNullOrWhiteSpace(dispositionName))
				{
					//从全局头里面查找当前上传文件的自定义属性
					foreach(var header in _headers)
					{
						if(header.Key.Length > dispositionName.Length + 1 && header.Key.StartsWith(dispositionName + "-", StringComparison.OrdinalIgnoreCase))
							fileInfo.Properties.Add(header.Key.Substring(dispositionName.Length + 1), header.Value);
					}
				}

				var infoKey = string.IsNullOrWhiteSpace(dispositionName) ? fileName : dispositionName;

				//将文件信息对象加入到集合中
				_fileData.Add(infoKey, fileInfo);

				try
				{
					return FileSystem.File.Open(filePath, FileMode.CreateNew, FileAccess.Write, (fileInfo.HasProperties ? fileInfo.Properties : null));
				}
				catch
				{
					if(fileInfo != null)
						_fileData.Remove(infoKey);

					throw;
				}
			}