/// <summary> /// Creates a new File /// </summary> /// <param name="camera">The camera this file is on (null if the file is a local file) /// A <see cref="Camera"/> /// </param> /// <param name="fs">The filesystem this file is on (null if the file is a local file) /// A <see cref="FileSystem"/> /// </param> /// <param name="metadata">The metadata for this file (string.Empty if there is none) /// A <see cref="System.String"/> /// </param> /// <param name="path">The path to this file /// A <see cref="System.String"/> /// </param> /// <param name="filename">The filename /// A <see cref="System.String"/> /// </param> /// <param name="local">True if the file is on the local filesystem, false if the file is on the camera /// A <see cref="System.Boolean"/> /// </param> protected File(Camera camera, FileSystem fs, string metadata, string path, string filename, bool local) { if (metadata == null) { throw new ArgumentNullException("metadata"); } this.camera = camera; this.fileName = filename; this.filesystem = fs; this.localFile = local; this.path = path; this.metadata = new Dictionary <string, string>(); this.mimetype = GuessMimetype(filename); ParseMetadata(metadata); if (local && !string.IsNullOrEmpty(filename)) { size = new FileInfo(System.IO.Path.Combine(path, filename)).Length; } else { size = (long)camera.Device.GetFileInfo(FileSystem.CombinePath(fs.BaseDirectory, path), filename, camera.Context).file.size; } }
/// <summary> /// Creates a new file from the supplied stream /// </summary> /// <param name="stream">The stream containing the file data /// A <see cref="Stream"/> /// </param> // public File (Stream stream) // : this(null, null, "", null, null, true) // { // if (stream == null) // throw new ArgumentNullException("stream"); // // this.stream = stream; // this.size = stream.Length; // } /// <summary> /// Reads the entire file and returns it as a byte array /// </summary> /// <returns> /// A <see cref="System.Byte"/> /// </returns> private byte[] DownloadBytes() { if (LocalFile) { throw new InvalidOperationException("This file is already on the local filesystem"); } string fullPath = FileSystem.CombinePath(filesystem.BaseDirectory, path); using (LibGPhoto2.CameraFile file = camera.Device.GetFile(fullPath, fileName, LibGPhoto2.CameraFileType.Normal, camera.Context)) return(file.GetDataAndSize()); }
internal static File Create(Camera camera, FileSystem fs, string directory, string filename) { LibGPhoto2.CameraFile metadataFile; string metadata = null; string mime = GuessMimetype(filename); LibGPhoto2.CameraFileType type = LibGPhoto2.CameraFileType.MetaData; string fullDirectory = FileSystem.CombinePath(fs.BaseDirectory, directory); using (metadataFile = camera.Device.GetFile(fullDirectory, filename, type, camera.Context)) metadata = Encoding.UTF8.GetString(metadataFile.GetDataAndSize()); /* First check to see if it's a music file */ if (mime == LibGPhoto2.MimeTypes.MP3 || mime == LibGPhoto2.MimeTypes.WMA || mime == LibGPhoto2.MimeTypes.WAV || mime == LibGPhoto2.MimeTypes.OGG) { return(new MusicFile(camera, fs, metadata, directory, filename, false)); } /* Second check to see if it's an image */ if (mime == LibGPhoto2.MimeTypes.BMP || mime == LibGPhoto2.MimeTypes.JPEG || mime == LibGPhoto2.MimeTypes.PNG || mime == LibGPhoto2.MimeTypes.RAW || mime == LibGPhoto2.MimeTypes.TIFF) { return(new ImageFile(camera, fs, metadata, directory, filename, false)); } /* Third check to see if it's a playlist */ if (filename.EndsWith(".zpl")) { // A playlist needs the actual data to work correctly as opposed to the metadata // this data is grabbed in the Playlist constructor return(new PlaylistFile(camera, fs, metadata, directory, filename, false)); } return(new GenericFile(camera, fs, metadata, directory, filename, false)); }
internal PlaylistFile(Camera camera, FileSystem fsystem, string metadata, string directory, string filename, bool local) : base(camera, fsystem, metadata, directory, filename, local) { string file; string filesystem; files = new List <Gphoto2.File>(); string fullDirectory = FileSystem.CombinePath(fsystem.BaseDirectory, directory); // The 'data' is a list of full filepaths seperated by newlines using (LibGPhoto2.CameraFile camfile = camera.Device.GetFile(fullDirectory, filename, LibGPhoto2.CameraFileType.Normal, camera.Context)) metadata = System.Text.Encoding.UTF8.GetString(camfile.GetDataAndSize()); StringReader r = new StringReader(metadata); while ((file = r.ReadLine()) != null) { FileSystem.SplitPath(file, out filesystem, out directory, out filename); FileSystem fs = camera.FileSystems.Find(delegate(FileSystem filesys) { return(filesys.BaseDirectory == filesystem); }); files.Add(File.Create(camera, fs, directory, filename)); } }
/// <summary> /// Synchronises the file metadata with the camera /// </summary> public void Update() { if (LocalFile) { throw new InvalidOperationException("Cannot update metadata on a local file"); } if (!IsDirty) { return; } string metadata = MetadataToXml(); using (LibGPhoto2.CameraFile file = new LibGPhoto2.CameraFile()) { file.SetFileType(LibGPhoto2.CameraFileType.MetaData); file.SetName(Filename); file.SetDataAndSize(System.Text.Encoding.UTF8.GetBytes(metadata)); camera.Device.PutFile(FileSystem.CombinePath(filesystem.BaseDirectory, path), file, camera.Context); } dirty = false; }