Skip to content

Commit

Permalink
Размер() и ПолучитьВремяИзменения().
Browse files Browse the repository at this point in the history
  • Loading branch information
dmpas committed Apr 12, 2017
1 parent 9eca077 commit 952edc1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
5 changes: 3 additions & 2 deletions TestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void Main(string[] args)
var process = engine.CreateProcess(new MainClass(), script);

var conn = FtpConnection.Constructor(ValueFactory.Create("10.2.150.7"), ValueFactory.Create(21),
ValueFactory.Create("update"), ValueFactory.Create("Black34")) as FtpConnection;
ValueFactory.Create("update"), ValueFactory.Create("")) as FtpConnection;
conn.SetCurrentDirectory("Storage1C");
Console.WriteLine("PWD: {0}", conn.GetCurrentDirectory());
conn.SetCurrentDirectory("Obmen");
Expand All @@ -42,7 +42,8 @@ public static void Main(string[] args)
foreach (var el in files)
{
var file = el as FtpFile;
Console.WriteLine("file: {0}", el);
Console.WriteLine("file: {0}, Size={1}, Time={2}", el, file.Size(), file.GetModificationTime());

if (first)
{
conn.Get(file.FullName, @"C:\temp\some.zip");
Expand Down
6 changes: 3 additions & 3 deletions oscript-ftp/FtpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Uri GetUri(string path)
return builder.Uri;
}

FtpWebRequest GetRequest(string path)
internal FtpWebRequest GetRequest(string path)
{
var uri = GetUri(path);
var request = (FtpWebRequest)WebRequest.Create(uri);
Expand Down Expand Up @@ -256,7 +256,7 @@ public ArrayImpl FindFiles(string path, string mask = null, bool recursive = tru

foreach (var dirName in directories)
{
var dirEntry = new FtpFile(path, dirName, isDir: true);
var dirEntry = new FtpFile(this, path, dirName, isDir: true);
if (maskChecker?.IsMatch(dirName) ?? true)
{
result.Add(dirEntry);
Expand All @@ -275,7 +275,7 @@ public ArrayImpl FindFiles(string path, string mask = null, bool recursive = tru
{
if (maskChecker?.IsMatch(fileName) ?? true)
{
var fileEntry = new FtpFile(path, fileName);
var fileEntry = new FtpFile(this, path, fileName);
result.Add(fileEntry);
}
}
Expand Down
52 changes: 51 additions & 1 deletion oscript-ftp/FtpFile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using ScriptEngine.Machine.Contexts;
using ScriptEngine.Machine;
using System.Net;

namespace oscriptFtp
{
Expand All @@ -11,14 +12,16 @@ namespace oscriptFtp
public sealed class FtpFile : AutoContext<FtpFile>
{
private readonly bool _isDirectory;
private readonly FtpConnection _conn;

/// <summary>
/// Создаёт описание файла.
/// </summary>
/// <param name="conn">FTP-Соединение.</param>
/// <param name="path">Путь к файлу.</param>
/// <param name="filename">Имя файла.</param>
/// <param name="isDir"><c>true</c> если это каталог.</param>
public FtpFile(string path, string filename, bool isDir = false)
public FtpFile(FtpConnection conn, string path, string filename, bool isDir = false)
{
Path = path;
if (!Path.EndsWith("/", StringComparison.Ordinal))
Expand All @@ -30,6 +33,7 @@ public FtpFile(string path, string filename, bool isDir = false)
Extension = System.IO.Path.GetExtension(Name);
BaseName = System.IO.Path.GetFileNameWithoutExtension(Name);
_isDirectory = isDir;
_conn = conn;
}

/// <summary>
Expand Down Expand Up @@ -87,6 +91,52 @@ public bool IsDirectory()
return _isDirectory;
}

/// <summary>
/// Получает время последнего изменения файла.
/// </summary>
/// <returns>Время последнего изменения.</returns>
[ContextMethod("ПолучитьВремяИзменения")]
public DateTime GetModificationTime()
{
FtpWebRequest request = _conn.GetRequest(FullName);
request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
return response.LastModified;
}

/// <summary>
/// Получает атрибут невидимости.
/// </summary>
/// <returns><c>true</c>, если у файла есть атрибут "Скрыты", <c>false</c> в противном случае.</returns>
[ContextMethod("ПолучитьНевидимость")]
public bool GetHidden()
{
throw new NotImplementedException();
}

/// <summary>
/// Получает атрибут "Только для чтения".
/// </summary>
/// <returns><c>true</c>, если установлен атрибут "Только для чтения", <c>false</c> в противном случае.</returns>
[ContextMethod("ПолучитьТолькоЧтение")]
public bool GetReadOnly()
{
throw new NotImplementedException();
}

/// <summary>
/// Определяет размер файла.
/// </summary>
/// <returns>Размер файла.</returns>
[ContextMethod("Размер")]
public long Size()
{
FtpWebRequest request = _conn.GetRequest(FullName);
request.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
return response.ContentLength;
}

/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:oscriptFtp.FtpFile"/>.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions oscript-ftp/oscript-ftp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\oscript-ftp.xml</DocumentationFile>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
Expand Down

0 comments on commit 952edc1

Please sign in to comment.