예제 #1
0
        /// <summary>
        /// Upload a file - path is the local folder path to save the file to which is relative to the local base directory.
        /// Not really interested in handling multiple files at the same time.
        /// </summary>
        /// <param name="path">string - a path in the form /Dir1/Dir2 to store the file into.</param>
        /// <returns>HttpResponse</returns>
        public async Task <HttpResponseMessage> Post(string path)
        {
            // Check whether the POST operation is MultiPart?
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            string baseSaveLocation = ConfigurationManager.AppSettings["DataDirectoryPath"];

            if (string.IsNullOrEmpty(baseSaveLocation))
            {
                Request.CreateErrorResponse(HttpStatusCode.InternalServerError, new HttpError("Server Configuration not correct, DataDirectoryPath not set."));
            }

            if (string.IsNullOrEmpty(path))
            {
                Request.CreateErrorResponse(HttpStatusCode.BadRequest, new HttpError("Path can not be an empty value."));
            }

            path = NetworkPath.Combine(baseSaveLocation, path);

            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
            catch (Exception e)
            {
                Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
            }

            CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(path);
            var files = new List <string>();

            try
            {
                // Read all contents of multipart message into CustomMultipartFormDataStreamProvider.
                await Request.Content.ReadAsMultipartAsync(provider);

                //await streamContent.ReadAsMultipartAsync(provider);

                files.AddRange(provider.FileData.Select(file => Path.GetFileName(file.LocalFileName)));

                // Send OK Response along with saved file names to the client.
                return(Request.CreateResponse(HttpStatusCode.OK, files));
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }
        /// <summary>
        /// DELETE: DELETE a directory at the specified path
        /// </summary>
        /// <param name="path">string - path to create</param>
        /// <returns>HttpResponseMessage</returns>
        public HttpResponseMessage Delete(string path)
        {
            string baseSaveLocation = ConfigurationManager.AppSettings["DataDirectoryPath"];
            string dirPath          = NetworkPath.Combine(baseSaveLocation, path);

            try
            {
                if (Directory.Exists(dirPath))
                {
                    Directory.Delete(dirPath, true);
                }
            }
            catch (Exception exception)
            {
                Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception);
            }

            return(Request.CreateResponse(HttpStatusCode.OK, path));
        }
        /// <summary>
        /// GET: api/{namespace}/Directory?Path={path}
        // Example path:  /root/path/to/directory/
        /// Gets the directory paths under the specified path
        /// </summary>
        /// <param name="path">string - sub path of direcory to get listing for.</param>
        /// <returns>list of strings - paths of directories under the target path </returns>
        public IEnumerable <string> Get(string path)
        {
            string baseSaveLocation = ConfigurationManager.AppSettings["DataDirectoryPath"];

            if (!Directory.Exists(baseSaveLocation))
            {
                Request.CreateErrorResponse(HttpStatusCode.InternalServerError, new HttpError("Path not found"));
                yield break;
            }

            var combine = NetworkPath.Combine(baseSaveLocation, path);

            if (Directory.Exists(combine))
            {
                DirectoryInfo di = new DirectoryInfo(combine);
                foreach (var directoryPath in di.GetDirectories())
                {
                    yield return(NetworkPath.Combine(path, directoryPath.Name));
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Get the file list for the directory
        /// /BRANCH/Dir
        /// </summary>
        /// <param name="Branch">Path of sub-directory to operation on</param>
        /// <param name="Dir">Specific directory to get the file listing</param>
        /// <returns>List of strings - file names</returns>
        private IEnumerable <string> FilesListing(string Branch, string Dir)
        {
            string baseSaveLocation = ConfigurationManager.AppSettings["DataDirectoryPath"];

            if (Directory.Exists(baseSaveLocation))
            {
                var combine = NetworkPath.Combine(baseSaveLocation, Branch);
                combine = NetworkPath.Combine(combine, Dir);
                if (Directory.Exists(combine))
                {
                    DirectoryInfo di = new DirectoryInfo(combine);
                    foreach (var filePath in di.GetFiles())
                    {
                        yield return(filePath.Name);
                    }
                }
                else
                {
                    Request.CreateErrorResponse(HttpStatusCode.SeeOther, new HttpError("Path not found"));
                }
            }
        }