Beispiel #1
0
        public async Task UploadFile(string path, string filename, string blobPath, string blobFilename, string contentType, bool deleteOnUpload = false, TimeSpan?ttl = null, bool append = false, CancellationToken token = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("filename");
            }

            if (string.IsNullOrEmpty(blobPath))
            {
                throw new ArgumentNullException("blobPath");
            }

            if (string.IsNullOrEmpty(blobFilename))
            {
                throw new ArgumentNullException("blobFilename");
            }

            if (string.IsNullOrEmpty(contentType))
            {
                throw new ArgumentNullException("contentType");
            }

            string fileToWrite = null;

            try
            {
                string fileToRead = operations.FixPath(path) + filename;
                string container  = operations.GetContainerName(blobPath);
                fileToWrite = operations.GetFilenameForContainer(blobPath, blobFilename);

                if (deleteOnUpload && !uploadQueue.ContainsKey(fileToWrite))
                {
                    uploadQueue.Add(fileToWrite, new LockFile()
                    {
                        IsLocked = true, TTL = ttl, Append = append, FileToRead = fileToRead, Container = container, ContentType = contentType
                    });
                }


                if (!token.IsCancellationRequested)
                {
                    byte[] source = await operations.ReadFileAsync(fileToRead, token);

                    if (source != null)
                    {
                        if (append)
                        {
                            string crlf      = "\r\n";
                            byte[] crlfBytes = Encoding.UTF8.GetBytes(crlf);
                            byte[] buffer    = new byte[crlfBytes.Length + source.Length];
                            Buffer.BlockCopy(crlfBytes, 0, buffer, 0, crlfBytes.Length);
                            Buffer.BlockCopy(source, 0, buffer, crlfBytes.Length, source.Length);
                            await operations.WriteAppendBlobAsync(container, fileToWrite, buffer, contentType, token);
                        }
                        else
                        {
                            await operations.WriteBlockBlobAsync(container, fileToWrite, source, contentType, token);
                        }

                        if (uploadQueue.ContainsKey(fileToWrite))
                        {
                            await operations.DeleteFileAsync(fileToWrite);

                            uploadQueue.Remove(fileToWrite);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                operations.LogToDocker("UploadFile", ex);

                if (uploadQueue.ContainsKey(fileToWrite))
                {
                    LockFile lf = uploadQueue[fileToWrite];
                    lf.IsLocked = false;
                    uploadQueue[fileToWrite] = lf;
                }
                else
                {
                    throw ex;
                }
            }
        }
        public async Task UploadFile(string path, string filename, string blobPath, string blobFilename, string contentType, bool deleteOnUpload = false, TimeSpan?ttl = null, bool append = false, CancellationToken token = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("filename");
            }

            if (string.IsNullOrEmpty(blobPath))
            {
                throw new ArgumentNullException("blobPath");
            }

            if (string.IsNullOrEmpty(blobFilename))
            {
                throw new ArgumentNullException("blobFilename");
            }

            if (string.IsNullOrEmpty(contentType))
            {
                throw new ArgumentNullException("contentType");
            }

            string fileToWrite = null;

            if (!Directory.Exists(path))
            {
                throw new DirectoryNotFoundException("Path does not exist.");
            }

            string fileToRead = operations.FixPath(path) + filename;

            if (!File.Exists(fileToRead))
            {
                throw new FileNotFoundException("Filename not found.");
            }

            try
            {
                string container = operations.GetContainerName(blobPath);
                fileToWrite = operations.GetFilenameForContainer(blobPath, blobFilename);

                if (deleteOnUpload && !uploadQueue.ContainsKey(fileToWrite))
                {
                    uploadQueue.Add(fileToWrite, new LockFile()
                    {
                        IsLocked = true, TTL = ttl, Append = append, FileToRead = fileToRead, Container = container, ContentType = contentType
                    });
                }


                if (!token.IsCancellationRequested)
                {
                    byte[] source = await operations.ReadFileAsync(fileToRead, token);

                    if (source != null)
                    {
                        if (append)
                        {
                            string crlf      = "\r\n";
                            byte[] crlfBytes = Encoding.UTF8.GetBytes(crlf);
                            byte[] buffer    = new byte[crlfBytes.Length + source.Length];
                            Buffer.BlockCopy(crlfBytes, 0, buffer, 0, crlfBytes.Length);
                            Buffer.BlockCopy(source, 0, buffer, crlfBytes.Length, source.Length);
                            await operations.WriteAppendBlobAsync(container, fileToWrite, buffer, contentType, token);
                        }
                        else
                        {
                            await operations.WriteBlockBlobAsync(container, fileToWrite, source, contentType, token);
                        }

                        if (uploadQueue.ContainsKey(fileToWrite))
                        {
                            try
                            {
                                await operations.DeleteFileAsync(fileToRead);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine($"Failed to deleted file (UploadFile) - {ex.Message}");
                                if (File.Exists(fileToRead))
                                {
                                    try
                                    {
                                        await Task.Delay(10000);

                                        File.Delete(fileToRead);
                                    }
                                    catch (Exception ex2)
                                    {
                                        Console.WriteLine($"Failed 2nd attempt to delete file (UploadFile) - {ex2.Message}");
                                    }
                                }
                            }
                            finally
                            {
                                uploadQueue.Remove(fileToWrite);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                operations.LogToDocker("UploadFile", ex);

                if (uploadQueue.ContainsKey(fileToWrite))
                {
                    LockFile lf = uploadQueue[fileToWrite];
                    lf.IsLocked = false;
                    uploadQueue[fileToWrite] = lf;
                }

                throw ex;
            }
        }
Beispiel #3
0
        public async Task UploadFile(string path, string filename, string blobPath, string blobFilename, string contentType, bool append = false, CancellationToken token = default(CancellationToken))
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("filename");
            }

            if (string.IsNullOrEmpty(blobPath))
            {
                throw new ArgumentNullException("blobPath");
            }

            if (string.IsNullOrEmpty(blobFilename))
            {
                throw new ArgumentNullException("blobFilename");
            }

            if (string.IsNullOrEmpty(contentType))
            {
                throw new ArgumentNullException("contentType");
            }

            try
            {
                string fileToRead  = operations.FixPath(path) + filename;
                string container   = operations.GetContainerName(blobPath);
                string fileToWrite = operations.GetFilenameForContainer(blobPath, blobFilename);

                if (!token.IsCancellationRequested)
                {
                    byte[] source = await operations.ReadFileAsync(fileToRead, token);

                    if (source != null)
                    {
                        if (append)
                        {
                            string crlf      = "\r\n";
                            byte[] crlfBytes = Encoding.UTF8.GetBytes(crlf);
                            byte[] buffer    = new byte[crlfBytes.Length + source.Length];
                            Buffer.BlockCopy(crlfBytes, 0, buffer, 0, crlfBytes.Length);
                            Buffer.BlockCopy(source, 0, buffer, crlfBytes.Length, source.Length);
                            await operations.WriteAppendBlobAsync(container, fileToWrite, buffer, contentType, token);
                        }
                        else
                        {
                            await operations.WriteBlockBlobAsync(container, fileToWrite, source, contentType, token);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                operations.LogToDocker("UploadFile", ex);
                throw ex;
            }
        }