Ejemplo n.º 1
0
        public async Task <FileUploadResultData> PostImage([FromForm] Guid id, IFormFile file)
        {
            using SqlConnection connection = this.userService.Connection;
            UserEntity user = await this.userDatabaseAccess.Get(this.userService.Username, connection);

            using DatabaseContext context = new DatabaseContext(connection);
            GroupEntity group = await context.ShakeGroup.FindAsync(id);

            if (group.OwnerId != user.Id)
            {
                throw new Exception("This post was made by another user.");
            }

            DateTime now = DateTime.Now;
            FileAccessTokenEntity token = await this.fileRepository.UploadInternal($"group_{now:yyyyMMdd'_'HHmmss}" + Path.GetExtension(file.FileName),
                                                                                   file.OpenReadStream(),
                                                                                   connection,
                                                                                   false);

            group.Icon = token;
            await context.SaveChangesAsync();

            connection.Close();

            return(new FileUploadResultData(token));
        }
Ejemplo n.º 2
0
 public static ClipboardGetData CreateWithImageContent(Guid id, Guid?laneId, FileAccessTokenEntity fileToken, string fileName)
 {
     return(new ClipboardGetData(id)
     {
         FileContentUrl = FileTokenData.CreateUrl(fileToken), LaneId = laneId, ContentTypeId = Constants.ContentTypes.Image, FileName = fileName
     });
 }
Ejemplo n.º 3
0
        /// <summary>
        /// The internal upload function.
        /// </summary>
        /// <param name="filename">The filename of the file to upload.</param>
        /// <param name="content">The content of the file to upload.</param>
        /// <param name="connection">The sql connection.</param>
        /// <param name="overwrite">True, if an existing file should be overwriten.</param>
        /// <returns>The token data of the uploaded file.</returns>
        public async Task <FileAccessTokenEntity> UploadInternal(string filename, Stream content, SqlConnection connection, bool overwrite)
        {
            this.GetContentTypeForExtension(Path.GetExtension(filename));

            FileAccessTokenEntity tokenEntity = await this.CreateTokenIfNotExists(filename, connection);

            BlobContainerClient azureContainer = await this.GetAzureContainer(this.userService.Username);

            BlobClient blob = azureContainer.GetBlobClient(filename);
            await blob.UploadAsync(content, overwrite);

            return(tokenEntity);
        }
        /// <summary>
        /// Creates the token for the file or retrives the existing one.
        /// </summary>
        /// <param name="fileName">The filename.</param>
        /// <param name="connection">The connection.</param>
        /// <returns>The token.</returns>
        public async Task <FileAccessTokenEntity> CreateTokenIfNotExists(string fileName, SqlConnection connection)
        {
            using DatabaseContext context = new DatabaseContext(connection);
            FileAccessTokenEntity existingToken = await this.TryUpdateToken(context, this.userService.UserId, fileName);

            if (existingToken != null)
            {
                return(existingToken);
            }

            FileAccessTokenEntity newToken = await CreateToken(fileName, this.userService.UserId, context);

            return(newToken);
        }
Ejemplo n.º 5
0
        private async Task <FileAccessTokenEntity> TryUpdateToken(DatabaseContext context, Guid userId, string fileName)
        {
            FileAccessTokenEntity token = await(from t in context.FileAccessToken
                                                where t.Filename == fileName &&
                                                t.User == userId
                                                select t).FirstOrDefaultAsync();

            if (token != null)
            {
                token.Token = GenerateToken();
                await context.SaveChangesAsync();
            }

            return(token);
        }
Ejemplo n.º 6
0
        private static async Task <FileAccessTokenEntity> CreateToken(string fileName, Guid userId, DatabaseContext context)
        {
            FileAccessTokenEntity token = new FileAccessTokenEntity()
            {
                Token    = GenerateToken(),
                Filename = fileName,
                User     = userId
            };

            await context.FileAccessToken.AddAsync(token);

            await context.SaveChangesAsync();

            return(token);
        }
Ejemplo n.º 7
0
        internal static ClipboardGetData CreateFromEntity(ClipboardContentEntity cc, FileAccessTokenEntity fileToken)
        {
            var contentType = cc.ContentType?.Id ?? cc.ContentTypeId;

            if (contentType == ContentTypes.Image)
            {
                return(CreateWithImageContent(cc.Id, cc.LaneId, fileToken, cc.DisplayFileName));
            }
            else if (contentType == ContentTypes.PlainText)
            {
                return(CreateWithPlainTextContent(cc.Id, cc.LaneId, cc.TextContent));
            }
            else if (contentType == ContentTypes.File)
            {
                return(CreateWithFileContent(cc.Id, cc.LaneId, fileToken, cc.DisplayFileName));
            }

            throw new Exception("Unexpected Content Type");
        }
Ejemplo n.º 8
0
        private async Task <ClipboardGetData> PostFileInternal(IFormFile file, string fileExtension, string fileName, Guid?laneId)
        {
            using SqlConnection connection = this.authService.Connection;

            DateTime now                = DateTime.Now;
            string   extension          = (fileExtension ?? Path.GetExtension(fileName) ?? Path.GetExtension(file.FileName));
            string   filename           = $"clip_{now:yyyyMMdd'_'HHmmss}" + extension;
            FileAccessTokenEntity token = await this.fileRepository.UploadInternal(filename,
                                                                                   file.OpenReadStream(),
                                                                                   connection,
                                                                                   false);

            Guid contentType = this.fileRepository.GetContentTypeForExtension(extension).StartsWith("image") ? Constants.ContentTypes.Image :
                               Constants.ContentTypes.File;

            ClipboardContentEntity entry = new ClipboardContentEntity()
            {
                ContentTypeId   = contentType,
                CreationDate    = now,
                LastUsedDate    = now,
                FileTokenId     = token.Id,
                UserId          = this.authService.UserId,
                DisplayFileName = fileName,
                LaneId          = laneId
            };

            using DatabaseContext context = new DatabaseContext(connection);
            await context.AddAsync(entry);

            await context.SaveChangesAsync();

            await connection.CloseAsync();

            connection.Close();

            return(contentType == Constants.ContentTypes.Image ? ClipboardGetData.CreateWithImageContent(entry.Id, entry.LaneId, token, fileName) :
                   ClipboardGetData.CreateWithFileContent(entry.Id, entry.LaneId, token, fileName));
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Creates a new instance of the <see cref="FileUploadResultData"/> class based on the given <see cref="FileAccessTokenEntity"/>.
 /// </summary>
 /// <param name="entity">The <see cref="FileAccessTokenEntity"/> to copy properties from.</param>
 public FileUploadResultData(FileAccessTokenEntity entity)
 {
     this.LocalUrl = FileTokenData.CreateUrl(entity);
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Generates the url.
 /// </summary>
 /// <param name="tokenEntity">The token entity.</param>
 /// <returns>The local url.</returns>
 public static string CreateUrl(FileAccessTokenEntity tokenEntity)
 {
     return(tokenEntity == null ? string.Empty : CreateUrl(tokenEntity.Token, tokenEntity.Filename));
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Create a new instance of the <see cref="FileTokenData"/> class from the given entity.
 /// </summary>
 /// <param name="entity">The base entity.</param>
 public FileTokenData(FileAccessTokenEntity entity)
 {
     this.Id       = entity.Id;
     this.Filename = entity.Filename;
     this.Token    = entity.Token.ToString("X");
 }