Example #1
0
        public string GetStorePath(string uploadForlderName,
                                   UploadLocationEnum uploadLocation             = UploadLocationEnum.LocalHost,
                                   UploadLinkAccessibilityEnum linkAccessibility = UploadLinkAccessibilityEnum.Private)
        {
            UploadFolderName = uploadForlderName;
            using (var scope = Services.CreateScope())
            {
                var httpContextAccessor =
                    scope.ServiceProvider
                    .GetRequiredService <IHttpContextAccessor>();

                var context =
                    scope.ServiceProvider
                    .GetRequiredService <DataContext>();

                User user = null;

                var refreshToken = httpContextAccessor.HttpContext?.Request.Cookies["refreshToken"];

                if (refreshToken != null)
                {
                    var token = context.Tokens.SingleOrDefault(x => x.RefreshToken == refreshToken);
                    user = token != null ? token.CreatedBy : null;
                }

                var userUploadSubFolderName = user != null ? user.UserName : "******";

                if (linkAccessibility == UploadLinkAccessibilityEnum.Public)
                {
                    if (uploadLocation == UploadLocationEnum.LocalHost)
                    {
                        var relativePath = user != null?
                                           Path.Combine("wwwroot", uploadForlderName, user.UserName) :
                                               Path.Combine("wwwroot", uploadForlderName, "unknown");

                        return(relativePath);
                    }

                    if (uploadLocation == UploadLocationEnum.FtpServer)
                    {
                        var relativePath = user != null?
                                           Path.Combine("public_html", uploadForlderName, user.UserName) :
                                               Path.Combine("public_html", uploadForlderName, "unknown");

                        return(relativePath);
                    }
                }

                var storePath = Path.Combine(uploadForlderName, userUploadSubFolderName);

                return(storePath);
            }
        }
Example #2
0
        public async Task SaveTusFileInfoAsync(ITusFile file, SiteTypeEnum siteType, string refreshToken, UploadLocationEnum location, UploadLinkAccessibilityEnum linkAccessibility, CancellationToken cancellationToken)
        {
            var metadata = await file.GetMetadataAsync(cancellationToken);

            string name = metadata["name"].GetString(Encoding.UTF8);
            string type = metadata["type"].GetString(Encoding.UTF8);


            using (var scope = Services.CreateScope())
            {
                var httpContextAccessor =
                    scope.ServiceProvider
                    .GetRequiredService <IHttpContextAccessor>();

                var context =
                    scope.ServiceProvider
                    .GetRequiredService <DataContext>();

                var logger =
                    scope.ServiceProvider
                    .GetRequiredService <IActivityLogger>();

                var token = context.Tokens.SingleOrDefault(x => x.RefreshToken == refreshToken);
                var user  = token.CreatedBy;

                var    storePath         = Path.Combine(UploadFolderName, user.UserName);
                var    extension         = Path.GetExtension(name);
                var    fileName          = file.Id;
                var    filePublicName    = name;
                var    mimeType          = type;
                var    fileRelativePath  = Path.Combine(storePath, fileName);
                long   size              = 0;
                string fileThumbnailPath = "";
                var    fileType          = TusCheckFileType(type);

                if (location == UploadLocationEnum.LocalHost)
                {
                    size = new FileInfo(Path.Combine(UploadFolderName, user.UserName, fileName)).Length;
                    fileThumbnailPath = fileType == AttachmentTypeEnum.Photo ? await GenerateImageThumbnailAsync(file, extension, storePath, user.UserName, cancellationToken) : "";
                }
                if (location == UploadLocationEnum.FtpServer)
                {
                    // create an FTP client
                    FtpClient client = new FtpClient(_baseUri, _port, _username, _password);
                    // begin connecting to the server
                    await client.ConnectAsync();

                    size = await client.GetFileSizeAsync($"{storePath}/{fileName}");

                    //
                    await client.DisconnectAsync();
                }

                var site = await context.Sites.SingleOrDefaultAsync(x => x.SiteType == siteType);

                user.CreatedAttachments.Add(new Attachment
                {
                    SiteId            = site.Id,
                    UploadLocation    = location,
                    Type              = fileType,
                    RelativePath      = fileRelativePath,
                    ThumbnailPath     = fileThumbnailPath,
                    MimeType          = mimeType,
                    FileName          = fileName,
                    PublicFileName    = filePublicName,
                    FileExtension     = extension,
                    FileSize          = size,
                    LinkAccessibility = linkAccessibility
                });

                var success = await context.SaveChangesAsync() > 0;

                if (success)
                {
                    await logger.LogActivity(
                        site.Id,
                        ActivityCodeEnum.AttachmentAdd,
                        ActivitySeverityEnum.Medium,
                        ActivityObjectEnum.Attachemnt,
                        $"The {fileType} file with the name {fileName} has been uploaded");
                }
                else
                {
                    throw new Exception("Problem saving file information in database!");
                }
            }
        }