Exemple #1
0
        public static FoundFile ScanForANewFile()
        {
            ShareClient share = new ShareClient(CONNECTIONSTRING, SHARENAME);

            foreach (ShareFileItem item in share.GetRootDirectoryClient().GetFilesAndDirectories())    // loop through all plants
            {
                if (item.Name == "System")
                {
                    continue;
                }
                if (item.IsDirectory)
                {
                    var subDirectory         = share.GetRootDirectoryClient().GetSubdirectoryClient(item.Name);
                    ShareDirectoryClient dir = subDirectory.GetSubdirectoryClient("immediateScan");
                    if (!dir.Exists())
                    {
                        continue;
                    }
                    foreach (var file in dir.GetFilesAndDirectories())
                    {
                        if (!file.IsDirectory)
                        {
                            string tempFileName = Path.GetTempFileName();
                            DownloadFile(dir.GetFileClient(file.Name), tempFileName);
                            return(new FoundFile(/*item.Name, */ file.Name, dir.Path + "/" + file.Name, tempFileName));
                        }
                    }
                }
            }
            return(null);
        }
Exemple #2
0
        public async Task UploadAsync(ConnectorConfig config, string fullFileName, MemoryStream stream)
        {
            var dirName  = Path.GetDirectoryName(fullFileName).ToString();
            var fileName = Path.GetFileName(fullFileName).ToString();

            ShareClient share = new ShareClient(config.connectionString, config.shareName);

            if (!share.Exists())
            {
                await share.CreateAsync();
            }

            ShareDirectoryClient directory = share.GetDirectoryClient(dirName);

            if (!directory.Exists())
            {
                await directory.CreateAsync();
            }

            ShareFileClient file = directory.GetFileClient(fileName);
            await file.CreateAsync(stream.Length);

            await file.UploadAsync(stream);

            /*
             * await file.UploadRange(
             *      ShareFileRangeWriteType.Update,
             *      new HttpRange(0, stream.Length),
             *      stream);*/
        }
Exemple #3
0
        private static async Task MoveFileAsync(ShareClient share, ShareDirectoryClient directory, ShareFileClient file, string outputFolder)
        {
            try
            {
                ShareDirectoryClient outputDirectory = share.GetDirectoryClient(outputFolder);

                await outputDirectory.CreateIfNotExistsAsync();

                if (outputDirectory.Exists())
                {
                    ShareFileClient outputFile = outputDirectory.GetFileClient(file.Name);
                    await outputFile.StartCopyAsync(file.Uri);

                    if (await outputFile.ExistsAsync())
                    {
                        Log($"{file.Uri} copied to {outputFile.Uri}");
                        await DeleteFileAsync(file);
                    }
                }
            }
            catch (Exception ex)
            {
                Log(ex);
            }
        }
        public IActionResult DownloadFile(string userId, string fileName)
        {
            // Get a reference to a share and then create it
            ShareClient share = new ShareClient(Secret.AzureConnectionString, Secret.AzureFileShareName);

            if (!share.Exists())
            {
                share.Create();
            }

            // Get a reference to a directory and create it
            ShareDirectoryClient directory = share.GetDirectoryClient(Auth0Utils.GetUserFolder());

            if (!directory.Exists())
            {
                directory.Create();
            }

            // Get a reference to a file and upload it
            ShareFileClient fileClient = directory.GetFileClient(fileName);

            if (fileClient.Exists())
            {
                //ShareFileDownloadInfo download = file.Download();
                return(File(fileClient.OpenRead(), "application/octet-stream"));
            }
            else
            {
                return(NotFound());
            }
        }
        public IActionResult DeleteFile(string userId, string fileName)
        {
            // Get a reference to a share and then create it
            ShareClient share = new ShareClient(Secret.AzureConnectionString, Secret.AzureFileShareName);

            if (!share.Exists())
            {
                share.Create();
            }

            // Get a reference to a directory and create it
            ShareDirectoryClient directory = share.GetDirectoryClient(Auth0Utils.GetUserFolder());

            if (!directory.Exists())
            {
                directory.Create();
            }

            // Get a reference to a file and upload it
            ShareFileClient fileClient = directory.GetFileClient(fileName);

            fileClient.DeleteIfExists();

            return(Ok());
        }
        public IActionResult Post(string userId)
        {
            try
            {
                IFormFileCollection files = Request.Form.Files;
                if (files.Count == 0)
                {
                    return(BadRequest());
                }

                // Get a reference to a share and then create it
                ShareClient share = new ShareClient(Secret.AzureConnectionString, Secret.AzureFileShareName);
                if (!share.Exists())
                {
                    share.Create();
                }

                // Get a reference to a directory and create it
                ShareDirectoryClient directory = share.GetDirectoryClient(Auth0Utils.GetUserFolder());
                if (!directory.Exists())
                {
                    directory.Create();
                }

                // Get a reference to a file and upload it
                ShareFileClient fileClient;

                foreach (IFormFile file in files)
                {
                    fileClient = directory.GetFileClient(file.FileName);

                    fileClient.DeleteIfExists();

                    using (Stream stream = file.OpenReadStream())
                    {
                        fileClient.Create(stream.Length);
                        fileClient.UploadRange(
                            new HttpRange(0, stream.Length),
                            stream);
                    }
                }

                return(Ok());
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }
Exemple #7
0
        /// <summary>
        /// Change the current directory
        /// </summary>
        /// <param name="directory">The name of the new current directory</param>
        /// <returns>True if successful. False if not. Any exceptions are thrown.</returns>
        public bool ChangeDirectory(string directory)
        {
            if (FileShare == null)
            {
                throw new Exception("Cannot change to Azure directory: " + directory + " (null share).");
            }

            // Get a reference to the directory.
            Directory = FileShare.GetDirectoryClient(directory);

            // Ensure that the directory exists.
            if (!Directory.Exists())
            {
                throw new Exception("Cannot change to Azure directory: " + directory + ".");
            }
            return(true);
        }
Exemple #8
0
        /// <summary>
        /// Copy a file from the current directory
        /// </summary>
        /// <param name="filename">The name of the file to copy</param>
        /// <param name="target">The name of the directory to copy the file to</param>
        /// <returns>Any exceptions are thrown.</returns>
        public void FileCopy(string filename, string target)
        {
            if (!this.FileExists(filename))
            {
                throw new Exception("Failed to Copy File: '" + filename + "' from '" + Directory.Name + ". File was missing.");
            }

            // Get a reference to the file
            ShareFileClient fileFrom = Directory.GetFileClient(filename);

            // Get a reference to the target directory.
            ShareDirectoryClient targetDir = FileShare.GetDirectoryClient(target);

            // Ensure that the directory exists.
            if (!targetDir.Exists())
            {
                throw new Exception("Cannot copy file to Azure directory: " + targetDir + ".");
            }
            try
            {
                ShareFileClient fileTo = targetDir.GetFileClient(filename);

                // Copy the file
                fileTo.StartCopy(fileFrom.Uri);
                ShareFileProperties props = fileTo.GetProperties();

                do
                {
                    Thread.Sleep(5);
                    //Console.WriteLine("Copy Progress: " + props.CopyProgress);
                } while (props.CopyStatus == CopyStatus.Pending);

                if (props.CopyStatus != CopyStatus.Success)
                {
                    throw new Exception("Failed to copy file: '" + filename + "' Error was: " + props.CopyStatusDescription);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to copy file: '" + filename + "' Error was: " + ex.Message);
            }
        }
Exemple #9
0
        /// <summary>
        /// Delete a directory
        /// </summary>
        /// <param name="directory">The name of the directory to be deleted</param>
        /// <returns>True if successful. False if not. Any exceptions are thrown.</returns>
        public bool DeleteDirectory(string directory)
        {
            if (FileShare == null)
            {
                throw new Exception("Cannot make Azure directory: " + directory + " (null share).");
            }

            // Get a reference to the directory.
            Directory = FileShare.GetDirectoryClient(directory);

            // Ensure that the directory exists.
            if (!Directory.Exists())
            {
                throw new Exception("Cannot delete Azure directory: " + directory + " - it doesn't exist.");
            }
            else
            {
                Directory.Delete();
            }
            return(true);
        }
        public IActionResult GetFileNames(string userId)
        {
            // Get a reference to a share and then create it
            ShareClient share = new ShareClient(Secret.AzureConnectionString, Secret.AzureFileShareName);

            if (!share.Exists())
            {
                share.Create();
            }

            // Get a reference to a directory and create it
            ShareDirectoryClient directory = share.GetDirectoryClient(Auth0Utils.GetUserFolder());

            if (!directory.Exists())
            {
                directory.Create();
            }

            var files = directory.GetFilesAndDirectories();

            return(Ok(files.ToList()));
        }