//******************************************************************************************************
// FtpFile.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 05/22/2003 - J. Ritchie Carroll
// Generated original version of source code.
// 08/06/2009 - Josh L. Patterson
// Edited Comments.
// 09/14/2009 - Stephen C. Wills
// Added new header and license agreement.
// 11/13/2003 - Pinal C. Patel
// Fixed null-reference exception occurrences in comparision methods/overloads.
// 12/17/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
#region [ Contributor License Agreements ]
//*******************************************************************************************************
//
// Code based on the following project:
// http://www.codeproject.com/KB/IP/net_ftp_upload.aspx
//
// Copyright Alex Kwok & Uwe Keim
//
// The Code Project Open License (CPOL):
// http://www.codeproject.com/info/cpol10.aspx
//
//*******************************************************************************************************
#endregion
using System;
namespace GSF.Net.Ftp
{
///
/// Represents a FTP file.
///
public class FtpFile : IFtpFile, IComparable
{
#region [ Members ]
// Fields
private readonly FtpDirectory m_parent;
private readonly string m_name;
private long m_size;
private string m_permission;
private DateTime m_timestamp;
#endregion
#region [ Constructors ]
internal FtpFile(FtpDirectory parent, FtpDirectory.ItemInfo info)
{
m_parent = parent;
m_name = info.Name;
m_size = info.Size;
m_permission = info.Permission;
m_timestamp = info.TimeStamp.Value;
}
internal FtpFile(FtpDirectory parent, string name)
{
m_parent = parent;
m_name = name;
}
#endregion
#region [ Properties ]
///
/// Name of file.
///
public string Name
{
get
{
return m_name;
}
}
///
/// Full path of file.
///
public string FullPath
{
get
{
return m_parent.FullPath + m_name;
}
}
///
/// Returns true for file entries.
///
public bool IsFile
{
get
{
return true;
}
}
///
/// Returns false for directory entries.
///
public bool IsDirectory
{
get
{
return false;
}
}
///
/// Gets or sets size of file.
///
public long Size
{
get
{
return m_size;
}
set
{
m_size = value;
}
}
///
/// Gets or sets permission of file.
///
public string Permission
{
get
{
return m_permission;
}
set
{
m_permission = value;
}
}
///
/// Gets or sets timestamp of file.
///
public DateTime Timestamp
{
get
{
return m_timestamp;
}
set
{
m_timestamp = value;
}
}
///
/// Gets parent directory of file.
///
public FtpDirectory Parent
{
get
{
m_parent.CheckSessionCurrentDirectory();
return m_parent;
}
}
#endregion
#region [ Methods ]
///
/// Downloads remote file.
///
public void Get()
{
Get(Name);
}
///
/// Downloads remote file using alternate local filename.
///
/// Local filename to use for download.
public void Get(string localFile)
{
string remoteFile = FtpPath.Combine(m_parent.FullPath, Name);
FtpFileTransferer transferer = new FtpFileTransferer(m_parent, localFile, remoteFile, Size, TransferDirection.Download);
transferer.StartTransfer();
}
///
/// Removes remote file.
///
public void Remove()
{
string remoteFile = $"{m_parent.FullPath}/{Name}";
m_parent.Session.ControlChannel.DELE(remoteFile);
m_parent.ClearItems();
}
///
/// Gets FTP input stream for file.
///
/// FTP input stream for file.
public FtpInputDataStream GetInputStream()
{
return ((FtpInputDataStream)(GetStream(0, TransferDirection.Download)));
}
///
/// Gets FTP output stream for file.
///
/// FTP output stream for file.
public FtpOutputDataStream GetOutputStream()
{
return ((FtpOutputDataStream)(GetStream(0, TransferDirection.Upload)));
}
///
/// Gets FTP input stream for file at given offset.
///
/// Offset into stream to start.
/// FTP input stream for file.
public FtpInputDataStream GetInputStream(long offset)
{
return ((FtpInputDataStream)(GetStream(offset, TransferDirection.Download)));
}
///
/// Gets FTP output stream for file at given offset.
///
/// Offset into stream to start.
/// FTP output stream for file.
public FtpOutputDataStream GetOutputStream(long offset)
{
return ((FtpOutputDataStream)(GetStream(offset, TransferDirection.Upload)));
}
private FtpDataStream GetStream(long offset, TransferDirection dir)
{
m_parent.CheckSessionCurrentDirectory();
FtpSessionConnected Session = m_parent.Session;
if (offset != 0)
Session.ControlChannel.REST(offset);
FtpDataStream stream = Session.ControlChannel.GetDataStream(dir);
try
{
if (dir == TransferDirection.Download)
Session.ControlChannel.RETR(m_name);
else
Session.ControlChannel.STOR(m_name);
}
catch
{
stream.Close();
throw;
}
return stream;
}
///
/// Determines if the two objects are equal.
///
/// Other object to compare.
/// true if both objects are equal.
public override bool Equals(object obj)
{
FtpFile other = obj as FtpFile;
if ((object)other != null)
return (CompareTo(other) == 0);
return false;
}
///
/// Generates hash code for this .
///
/// An representing the hash code.
public override int GetHashCode()
{
return m_name.GetHashCode();
}
int IComparable.CompareTo(FtpFile other)
{
// Files are sorted by name
return string.Compare(m_name, other.Name, m_parent.CaseInsensitive);
}
int IComparable.CompareTo(IFtpFile other)
{
// Files are sorted by name
return string.Compare(m_name, other.Name, m_parent.CaseInsensitive);
}
///
/// Compares directory or file to another.
///
/// An to compare against.
/// An that represents the result of the comparison. 1 - object is greater than, 0 - object is equal to, -1 - object is less than.
public int CompareTo(object obj)
{
if (Equals(obj, null))
{
return 1;
}
else
{
IFtpFile file = obj as IFtpFile;
if ((object)file == null)
return 1;
else
return CompareTo(file);
}
}
#endregion
#region [ Operators ]
///
/// Compares the two values for equality.
///
/// A as the left hand operand.
/// A as the right hand operand.
/// A value indicating the result of the comparison.
public static bool operator ==(FtpFile value1, FtpFile value2)
{
if (Equals(value1, null))
return Equals(value2, null);
else
return (value1.CompareTo(value2) == 0);
}
///
/// Compares the two values for inequality.
///
/// A as the left hand operand.
/// A as the right hand operand.
/// A value indicating the result of the comparison.
public static bool operator !=(FtpFile value1, FtpFile value2)
{
return !(value1 == value2);
}
///
/// Returns true if left value is less than right value.
///
/// A as the left hand operand.
/// A as the right hand operand.
/// A value indicating the result of the comparison.
public static bool operator <(FtpFile value1, FtpFile value2)
{
if (Equals(value1, null))
return Equals(value2, null);
else
return (value1.CompareTo(value2) < 0);
}
///
/// Returns true if left value is greater than right value.
///
/// A as the left hand operand.
/// A as the right hand operand.
/// A value indicating the result of the comparison.
public static bool operator >(FtpFile value1, FtpFile value2)
{
return !(value1 <= value2);
}
///
/// Returns true if left value is less or equal to than right value.
///
/// A as the left hand operand.
/// A as the right hand operand.
/// A value indicating the result of the comparison.
public static bool operator <=(FtpFile value1, FtpFile value2)
{
if (Equals(value1, null))
return Equals(value2, null);
else
return (value1.CompareTo(value2) <= 0);
}
///
/// Returns true if left value is greater than or equal to right value.
///
/// A as the left hand operand.
/// A as the right hand operand.
/// A value indicating the result of the comparison.
public static bool operator >=(FtpFile value1, FtpFile value2)
{
return !(value1 < value2);
}
#endregion
}
}