/// <summary> /// Uploads the specified file into the specified path saving it with the specified /// filename /// </summary> /// <param name="file">The file to upload /// A <see cref="File"/> /// </param> /// <param name="directory">The path where the file should be uploaded to /// A <see cref="System.String"/> /// </param> /// <param name="filename">The filename to save the file as on the filesystem /// A <see cref="System.String"/> /// </param> /// <returns>An object representing the file on the camera /// A <see cref="File"/> /// </returns> public File Upload(File file, string directory, string filename) { if (file == null) { throw new ArgumentNullException("file"); } if (string.IsNullOrEmpty(filename)) { throw new ArgumentException("filename cannot be null or empty"); } if (!Contains(directory)) { CreateDirectory(directory); } string fullPath = CombinePath(BaseDirectory, directory); // First put the actual file data on the camera using (LibGPhoto2.CameraFile data = new LibGPhoto2.CameraFile()) { data.SetName(filename); data.SetFileType(LibGPhoto2.CameraFileType.Normal); data.SetDataAndSize(System.IO.File.ReadAllBytes(Path.Combine(file.Path, file.Filename))); data.SetMimeType(file.MimeType); camera.Device.PutFile(fullPath, data, camera.Context); } // Then put the metadata on camera. using (LibGPhoto2.CameraFile meta = new LibGPhoto2.CameraFile()) { meta.SetName(filename); meta.SetFileType(LibGPhoto2.CameraFileType.MetaData); meta.SetDataAndSize(System.Text.Encoding.UTF8.GetBytes(file.MetadataToXml())); camera.Device.PutFile(fullPath, meta, camera.Context); } // Then return the user a File object referencing the file on the camera // FIXME: Hack to copy the metadata correctly. Libgphoto returns null // metadata until the device refreshes it's database. Workaround is to manually // copy the metadata over from the old file. File returnFile = GetFileInternal(directory, filename); returnFile.Metadata.Clear(); foreach (KeyValuePair <string, string> kp in file.Metadata) { returnFile.Metadata.Add(kp.Key, kp.Value); } // FIXME: This is another hack to fix the above issue returnFile.Size = file.Size; return(returnFile); }
/// <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 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; }
/// <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; }
/// <summary> /// Uploads the specified file into the specified path saving it with the specified /// filename /// </summary> /// <param name="file">The file to upload /// A <see cref="File"/> /// </param> /// <param name="directory">The path where the file should be uploaded to /// A <see cref="System.String"/> /// </param> /// <param name="filename">The filename to save the file as on the filesystem /// A <see cref="System.String"/> /// </param> /// <returns>An object representing the file on the camera /// A <see cref="File"/> /// </returns> public File Upload(File file, string directory, string filename) { if (file == null) throw new ArgumentNullException("file"); if (string.IsNullOrEmpty(filename)) throw new ArgumentException("filename cannot be null or empty"); if(!Contains(directory)) CreateDirectory(directory); string fullPath = CombinePath(BaseDirectory, directory); // First put the actual file data on the camera using (LibGPhoto2.CameraFile data = new LibGPhoto2.CameraFile()) { data.SetName(filename); data.SetFileType(LibGPhoto2.CameraFileType.Normal); data.SetDataAndSize(System.IO.File.ReadAllBytes(Path.Combine(file.Path, file.Filename))); data.SetMimeType(file.MimeType); camera.Device.PutFile(fullPath, data, camera.Context); } // Then put the metadata on camera. using (LibGPhoto2.CameraFile meta = new LibGPhoto2.CameraFile()) { meta.SetName(filename); meta.SetFileType(LibGPhoto2.CameraFileType.MetaData); meta.SetDataAndSize(System.Text.Encoding.UTF8.GetBytes(file.MetadataToXml())); camera.Device.PutFile(fullPath, meta, camera.Context); } // Then return the user a File object referencing the file on the camera // FIXME: Hack to copy the metadata correctly. Libgphoto returns null // metadata until the device refreshes it's database. Workaround is to manually // copy the metadata over from the old file. File returnFile = GetFileInternal(directory, filename); returnFile.Metadata.Clear(); foreach (KeyValuePair<string, string> kp in file.Metadata) returnFile.Metadata.Add(kp.Key, kp.Value); // FIXME: This is another hack to fix the above issue returnFile.Size = file.Size; return returnFile; }