Ejemplo n.º 1
0
        protected override Task <HttpResponseMessage> ListTransportSpecificFileAsync(string folderPath)
        {
            Collection <TC.FileInfo> files = new Collection <TC.FileInfo>();

            return(Task.Factory.StartNew(() =>
            {
                if (!this.ValidateController())
                {
                    return this.Request.CreateResponse(HttpStatusCode.BadRequest, this.validationErrors);
                }

                try
                {
                    using (Session session = new Session())
                    {
                        HttpResponseMessage response;
                        if ((response = this.OpenSession(session)).StatusCode != HttpStatusCode.OK)
                        {
                            return response;
                        }

                        RemoteDirectoryInfo remoteDirectoryInfo = session.ListDirectory(folderPath);

                        for (int i = 0; i < remoteDirectoryInfo.Files.Count; i++)
                        {
                            string fileName = remoteDirectoryInfo.Files[i].Name;

                            if (!fileName.Equals(".") && !fileName.Equals(".."))
                            {
                                TC.FileInfo info = new TC.FileInfo(this.ServerAddress, folderPath, fileName);
                                files.Add(info);
                            }
                        }
                    }

                    Trace.TraceInformation("ListFile returning successfully, {0} ", folderPath);
                    return Request.CreateResponse(files);
                }
                catch (SessionRemoteException ex)
                {
                    return GetErrorResponseMessage(ex, folderPath);
                }
            }));
        }
Ejemplo n.º 2
0
        protected override Task <HttpResponseMessage> GetTransportSpecificFileAsync(string folderPath, string fileName, ContentTransferEncoding contentTransferEncoding)
        {
            string remotePath    = folderPath + "/" + fileName;
            string temporaryPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()) + "\\";

            return(Task.Factory.StartNew(() =>
            {
                if (!this.ValidateController())
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest, this.validationErrors);
                }

                try
                {
                    Directory.CreateDirectory(temporaryPath);

                    using (Session session = new Session())
                    {
                        HttpResponseMessage response;
                        if ((response = this.OpenSession(session)).StatusCode != HttpStatusCode.OK)
                        {
                            return response;
                        }

                        session.GetFiles(remotePath, temporaryPath).Check();
                    }

                    TC.FileInfo fileInfo = new TC.FileInfo(this.ServerAddress, folderPath, fileName);
                    TC.File message;

                    // Todo : Change MemoryStream to VirtualFileStream
                    using (MemoryStream stream = new MemoryStream())
                    {
                        using (FileStream fileStream = System.IO.File.OpenRead(temporaryPath + "/" + fileName))
                        {
                            stream.SetLength(fileStream.Length);
                            fileStream.Read(stream.GetBuffer(), 0, (int)fileStream.Length);
                        }

                        stream.Position = 0;
                        message = new TC.File(stream, fileInfo, contentTransferEncoding);
                    }

                    Trace.TraceInformation("GetFile returning successfully, {0} , {1} ", folderPath, fileName);
                    return Request.CreateResponse(message);
                }
                catch (SessionRemoteException ex)
                {
                    return GetErrorResponseMessage(ex, remotePath);
                }
                catch (IOException ex)
                {
                    Trace.TraceError("Stacktrace :" + ex.StackTrace);
                    return Request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, Resources.ServiceUnavailable);
                }
                finally
                {
                    Directory.Delete(temporaryPath, true);
                }
            }));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Upload Util Method for SFTP
        /// </summary>
        /// <param name="content"></param>
        /// <param name="folderPath"></param>
        /// <param name="fileName"></param>
        /// <param name="appendIfExist"></param>
        /// <param name="temporaryFolder"></param>
        /// <returns></returns>
        protected override async Task <HttpResponseMessage> UploadTransportSpecificFileAsync(FileContent content, string folderPath, string fileName, bool appendIfExist, string temporaryFolder)
        {
            if (!string.IsNullOrWhiteSpace(temporaryFolder) && appendIfExist)
            {
                throw new ArgumentException(Resources.InvalidOperationWithTemporaryFolder, "temporaryFolder");
            }

            if (!this.ValidateController())
            {
                return(this.Request.CreateResponse(HttpStatusCode.BadRequest, this.validationErrors));
            }

            string path          = folderPath;
            string temporaryPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()) + "\\";

            try
            {
                Directory.CreateDirectory(temporaryPath);

                using (Session session = new Session())
                {
                    HttpResponseMessage response;
                    if ((response = this.OpenSession(session)).StatusCode != HttpStatusCode.OK)
                    {
                        return(response);
                    }

                    if (string.IsNullOrWhiteSpace(temporaryFolder))
                    {
                        await this.UploadToFolder(session, content.GetStream(), folderPath, fileName, temporaryPath, appendIfExist);
                    }
                    else
                    {
                        path = temporaryFolder;
                        await this.UploadToFolder(session, content.GetStream(), temporaryFolder, fileName, temporaryPath);

                        string tempPath        = temporaryFolder + "/" + fileName;
                        string destinationPath = folderPath + "/" + fileName;

                        if (session.FileExists(destinationPath))
                        {
                            session.RemoveFiles(destinationPath);
                        }

                        try
                        {
                            session.MoveFile(tempPath, destinationPath);
                        }
                        catch (SessionRemoteException ex)
                        {
                            Trace.TraceError("Stacktrace :" + ex.StackTrace);
                            return(Request.CreateErrorResponse(HttpStatusCode.NotFound, string.Format(
                                                                   CultureInfo.InvariantCulture,
                                                                   Resources.SftpAdapter_NotFound, destinationPath), ex));
                        }
                    }
                }

                TC.FileInfo fileInfo = new TC.FileInfo(this.ServerAddress, folderPath, fileName);
                Trace.TraceInformation("UploadFile returning successfully, {0} , {1} ", folderPath, fileName);
                return(Request.CreateResponse(fileInfo));
            }
            catch (SessionRemoteException ex)
            {
                return(GetErrorResponseMessage(ex, path));
            }
            catch (PathTooLongException ex)
            {
                Trace.TraceError("Stacktrace :" + ex.StackTrace);
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message));
            }
            catch (IOException ex)
            {
                Trace.TraceError("Stacktrace :" + ex.StackTrace);
                return(Request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, Resources.ServiceUnavailable));
            }
            finally
            {
                Directory.Delete(temporaryPath, true);
            }
        }