private void ValidateIfMatch() { var headers = _context.Request.Headers; if (headers.ContainsKey(HeaderNames.IfMatch)) { var ifMatch = headers[HeaderNames.IfMatch].ToString().Trim(); if (!ifMatch.Equals(ETag.Create(_file).Value)) { throw new PreconditionFailedException(HeaderNames.IfMatch); } } }
private void AddFileContentHeaders(ETag etag) { // // Content Type string type = null; if (!MimeMaps.TryGetContentType(Path.GetExtension(_file.Name), out type)) { type = "application/octet-stream"; } _context.Response.ContentType = type; // // Content-Disposition (file name) _context.Response.Headers.SetContentDisposition(false, _file.Name); // // Accept Ranges _context.Response.Headers.Add(HeaderNames.AcceptRanges, "bytes"); // // Last Modified _context.Response.Headers.Add(HeaderNames.LastModified, _file.LastModified.ToUniversalTime().ToString("r")); // // ETag _context.Response.Headers.Add(HeaderNames.ETag, etag.Value); if (IsCachedIfModifiedSince()) { // // Date if (!_context.Response.Headers.ContainsKey(HeaderNames.Date)) { _context.Response.Headers.Add(HeaderNames.Date, DateTime.UtcNow.ToString()); } } SetCustomHeaders(); }
// // Accept parent to optimize serialization performance private object FileToJsonModel(IFileInfo info, Fields fields = null, bool full = true, IFileInfo parent = null) { if (fields == null) { fields = Fields.All; } dynamic obj = new ExpandoObject(); var fileId = FileId.FromPhysicalPath(info.Path); bool? exists = null; // // name if (fields.Exists("name")) { obj.name = info.Name; } // // id if (fields.Exists("id")) { obj.id = fileId.Uuid; } // // alias if (fields.Exists("alias")) { foreach (var location in _fileProvider.Options.Locations) { if (location.Path.TrimEnd(PathUtil.SEPARATORS).Equals(info.Path.TrimEnd(PathUtil.SEPARATORS), StringComparison.OrdinalIgnoreCase)) { obj.alias = location.Alias; break; } } } // // type if (fields.Exists("type")) { obj.type = Enum.GetName(typeof(FileType), FileType.File).ToLower(); } // // physical_path if (fields.Exists("physical_path")) { obj.physical_path = info.Path; } // // exists if (fields.Exists("exists")) { exists = exists ?? info.Exists; obj.exists = exists.Value; } // // size if (fields.Exists("size")) { exists = exists ?? info.Exists; obj.size = exists.Value ? info.Size : 0; } // // created if (fields.Exists("created")) { exists = exists ?? info.Exists; obj.created = exists.Value ? (object)info.Created.ToUniversalTime() : null; } // // last_modified if (fields.Exists("last_modified")) { exists = exists ?? info.Exists; obj.last_modified = exists.Value ? (object)info.LastModified.ToUniversalTime() : null; } // // last_access if (fields.Exists("last_access")) { exists = exists ?? info.Exists; obj.last_access = exists.Value ? (object)info.LastAccessed.ToUniversalTime() : null; } // // mime_type if (fields.Exists("mime_type")) { string type = null; HttpFileHandler.MimeMaps.TryGetContentType(info.Path, out type); obj.mime_type = type; } // // e_tag if (fields.Exists("e_tag")) { exists = exists ?? info.Exists; obj.e_tag = exists.Value ? ETag.Create(info).Value : null; } // // parent if (fields.Exists("parent")) { obj.parent = parent != null?DirectoryToJsonModelRef(parent, fields.Filter("parent")) : GetParentJsonModelRef(info.Path, fields.Filter("parent")); } // // claims if (fields.Exists("claims")) { obj.claims = info.Claims; } return(Core.Environment.Hal.Apply(Defines.FilesResource.Guid, obj, full)); }
public async Task PutFileContent() { _service.EnsureAccess(_file.Path, FileAccess.Write); int start, finish, outOf; var etag = ETag.Create(_file); ValidateIfMatch(); ValidateIfNoneMatch(etag); ValidateIfUnmodifiedSince(); ValidateContentRange(out start, out finish, out outOf); IFileInfo tempFile = CreateTempFile(_file.Path); try { using (var temp = _service.GetFileStream(tempFile, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) { // // Write to temp file await _context.Request.Body.CopyToAsync(temp); temp.Flush(); temp.Seek(0, SeekOrigin.Begin); // // Copy from temp using (var real = TryOpenFile(_file, FileMode.Open, FileAccess.Write, FileShare.Read)) { if (start >= 0) { // // Range request real.Seek(start, SeekOrigin.Begin); } await temp.CopyToAsync(real); // Truncate content-range if (finish > 0 && finish == outOf - 1) { real.SetLength(outOf); } // Truncate full content if (start == -1) { real.SetLength(temp.Length); } // // https://github.com/dotnet/corefx/blob/ec2a6190efa743ab600317f44d757433e44e859b/src/System.IO.FileSystem/src/System/IO/FileStream.Win32.cs#L1687 // Unlike Flush(), FlushAsync() always flushes to disk. This is intentional. real.Flush(); } } } catch (IndexOutOfRangeException) { throw new ApiArgumentException(HeaderNames.ContentLength); } finally { _service.Delete(tempFile); } _context.Response.StatusCode = (int)HttpStatusCode.OK; }
private object FileToJsonModel(IFileInfo info, Fields fields = null, bool full = true) { if (fields == null) { fields = Fields.All; } dynamic obj = new ExpandoObject(); var fileId = FileId.FromPhysicalPath(info.Path); bool? exists = null; // // name if (fields.Exists("name")) { obj.name = info.Name; } // // id if (fields.Exists("id")) { obj.id = fileId.Uuid; } // // type if (fields.Exists("type")) { obj.type = Enum.GetName(typeof(FileType), FileType.File).ToLower(); } // // physical_path if (fields.Exists("physical_path")) { obj.physical_path = info.Path; } // // exists if (fields.Exists("exists")) { exists = exists ?? info.Exists; obj.exists = exists.Value; } // // size if (fields.Exists("size")) { exists = exists ?? info.Exists; obj.size = exists.Value ? info.Size : 0; } // // created if (fields.Exists("created")) { exists = exists ?? info.Exists; obj.created = exists.Value ? (object)info.Created.ToUniversalTime() : null; } // // last_modified if (fields.Exists("last_modified")) { exists = exists ?? info.Exists; obj.last_modified = exists.Value ? (object)info.LastModified.ToUniversalTime() : null; } // // last_access if (fields.Exists("last_access")) { exists = exists ?? info.Exists; obj.last_access = exists.Value ? (object)info.LastAccessed.ToUniversalTime() : null; } // // e_tag if (fields.Exists("e_tag")) { exists = exists ?? info.Exists; obj.e_tag = exists.Value ? ETag.Create(info).Value : null; } // // parent if (fields.Exists("parent")) { obj.parent = GetParentJsonModelRef(info.Path, fields.Filter("parent")); } // // claims if (fields.Exists("claims")) { obj.claims = _fileProvider.GetClaims(info.Path); } return(Core.Environment.Hal.Apply(Defines.FilesResource.Guid, obj, full)); }