public static async Task <CustomFileModel> CustomCreateAsync(ICustomFile file, RootVolume volume) { if (file == null) { throw new ArgumentNullException(nameof(file)); } if (volume == null) { throw new ArgumentNullException(nameof(volume)); } var parentPath = file.DirectoryName.Substring(volume.RootDirectory.Length); var relativePath = file.FullName.Substring(volume.RootDirectory.Length); var fileProperties = await file.PropertiesAsync; var response = new CustomFileModel { UnixTimeStamp = (long)(fileProperties.LastModified.DateTime - unixOrigin).TotalSeconds, Read = 1, Write = volume.IsReadOnly ? (byte)0 : (byte)1, Locked = volume.LockedFolders != null && volume.LockedFolders.Any(f => f == file.Directory.Name) || volume.IsLocked ? (byte)1 : (byte)0, Name = file.Name, Size = fileProperties.ContentLength, Mime = MimeHelper.GetMimeType(file.Extension), Hash = volume.VolumeId + HttpEncoder.EncodePath(relativePath), ParentHash = volume.VolumeId + HttpEncoder.EncodePath(parentPath.Length > 0 ? parentPath : file.Directory.Name) }; // We don't download and create thumbnails for files bigger than 2Mb if (!volume.CanCreateThumbnail(file) || fileProperties.ContentLength <= 0L || fileProperties.ContentLength > 2000000) { return(response); } var filePath = $"{file.Directory.FullName}/{Path.GetFileNameWithoutExtension(file.Name)}"; // Remove first segment of the path before the first '/' filePath = filePath.Substring(filePath.IndexOf('/')); // Add ticks to be sure that the thumbnail will be re-created if an image with the same filename will be uploaded again var str = filePath + "_" + fileProperties.CreatedOn.Ticks + file.Extension; response.Thumbnail = HttpEncoder.EncodePath(str); return(response); }
private void CustomFiles_Removing(int index, ICustomFile value) { XElement root = DocumentManifest.Manifest.Element(Ns.Manifest + "manifest"); IEnumerable <XElement> fileEntries = root.Elements(Ns.Manifest + "file-entry").Where( e => ((string)e.Attribute(Ns.Manifest + "media-type")).Equals(value.MediaType ?? string.Empty, StringComparison.InvariantCulture) && ((string)e.Attribute(Ns.Manifest + "full-path")).Equals(value.FullPath, StringComparison.InvariantCulture)); foreach (XElement fileEntry in fileEntries) { fileEntry.Remove(); } }
private void WriteCustomFile(ICustomFile file) { using (Stream targetStream = _packageWriter.Open(file.FullPath)) { using (Stream sourceStream = file.OpenRead()) { byte[] buffer = new byte[1024]; while (true) { int read = sourceStream.Read(buffer, 0, buffer.Length); if (read <= 0) { return; } targetStream.Write(buffer, 0, read); } } } }
private void CustomFiles_Inserting(int index, ICustomFile value) { XElement root = DocumentManifest.Manifest.Element(Ns.Manifest + "manifest"); IEnumerable <XElement> fileEntries = root.Elements(Ns.Manifest + "file-entry").Where( e => ((string)e.Attribute(Ns.Manifest + "media-type")).Equals(value.MediaType ?? string.Empty, StringComparison.InvariantCulture) && ((string)e.Attribute(Ns.Manifest + "full-path")).Equals(value.FullPath, StringComparison.InvariantCulture)); if (fileEntries.Count() > 0) { return; } XElement fileEntry = new XElement(Ns.Manifest + "file-entry"); fileEntry.SetAttributeValue(Ns.Manifest + "media-type", value.MediaType ?? string.Empty); fileEntry.SetAttributeValue(Ns.Manifest + "full-path", value.FullPath); root.Add(fileEntry); }