Ejemplo n.º 1
0
        public async Task <List <string> > UploadPackageAsync(UploadPackageRequest request)
        {
            FileInfo[]    filesInDir;
            DirectoryInfo directoryInWhichToSearch = new DirectoryInfo(request.DirectoryPath);

            if (!string.IsNullOrEmpty(request.PackageSearchPattern))
            {
                filesInDir = directoryInWhichToSearch.GetFiles(request.PackageSearchPattern);
            }
            else
            {
                filesInDir = directoryInWhichToSearch.GetFiles();
            }

            if (filesInDir.Length == 0)
            {
                return(new List <string>());
            }

            List <string> uploadedFiles = new List <string>();

            using (var content = new MultipartFormDataContent())
            {
                ////todo investigate why one content has to be added.
                var json = JsonConvert.SerializeObject(request);
                content.Add(new StringContent(json), "request");
                foreach (var file in filesInDir)
                {
                    uploadedFiles.Add(file.FullName);
                    var    stream   = new FileStream(file.FullName, FileMode.Open);
                    string fileName = Path.GetFileName(file.FullName);
                    content.Add(new StreamContent(stream), fileName, fileName);
                }

                Client.DefaultRequestHeaders.Authorization = !string.IsNullOrEmpty(Token)
                    ? new AuthenticationHeaderValue("Bearer", Token)
                    : null;
                var response = await Client.PostAsync(new Uri(string.Format("{0}api/packages", WebApiBaseUrl)), content);

                await GetResponse <Void>(response);

                return(uploadedFiles);
            }
        }
Ejemplo n.º 2
0
        private string GetUploadDirectory()
        {
            var form            = Request.Form;
            var uploadDirectory = Path.Combine(_hostingEnvironment.ContentRootPath, "Packages");

            if (form.ContainsKey("request"))
            {
                StringValues         request = form["request"];
                var                  json    = request[0];
                UploadPackageRequest uploadPackageRequest = null;
                try
                {
                    uploadPackageRequest = JsonConvert.DeserializeObject <UploadPackageRequest>(json);
                }
                catch (System.Exception e)
                {
                    _logger.LogWarning(
                        $"request was present but was not of type UploadPackageRequest. Package will be uploaded to root directory. Excetpion: {e}");
                    return(uploadDirectory);
                }

                if (!string.IsNullOrWhiteSpace(uploadPackageRequest.UploadToSubDirectory))
                {
                    var destDirPath = Path.GetFullPath(Path.Combine(uploadDirectory + Path.DirectorySeparatorChar));
                    uploadDirectory = Path.GetFullPath(Path.Combine(uploadDirectory, uploadPackageRequest.UploadToSubDirectory));

                    if (!uploadDirectory.StartsWith(destDirPath))
                    {
                        throw new HttpError(HttpStatusCode.Forbidden);
                    }
                }
            }

            if (!Directory.Exists(uploadDirectory))
            {
                Directory.CreateDirectory(uploadDirectory);
            }

            return(uploadDirectory);
        }