Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mall.oytour.com
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
黄奎
mall.oytour.com
Commits
59ad934e
Commit
59ad934e
authored
Oct 15, 2020
by
liudong1993
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
小程序上传调整封装
parent
4933720f
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
124 additions
and
30 deletions
+124
-30
Config.cs
Mall.Common/Config.cs
+33
-0
RB_File_StoreRepository.cs
Mall.Repository/BaseSetUp/RB_File_StoreRepository.cs
+5
-1
AliOssService.cs
Mall.ThirdCore/Oss/AliOssService.cs
+14
-14
OssService.cs
Mall.ThirdCore/Oss/OssService.cs
+11
-2
TencentOss.cs
Mall.ThirdCore/Oss/TencentOss.cs
+6
-6
FileController.cs
Mall.WebApi/Controllers/File/FileController.cs
+55
-7
No files found.
Mall.Common/Config.cs
View file @
59ad934e
...
...
@@ -203,6 +203,12 @@ namespace Mall.Common
}
}
public
static
string
GetAliOssFileUrl
{
get
{
return
ReadConfigKey
(
"AliFileSiteUrl"
);
}
}
/// <summary>
/// 获取Oss文件全路径
/// </summary>
...
...
@@ -230,6 +236,33 @@ namespace Mall.Common
}
}
/// <summary>
/// 获取Oss文件全路径
/// </summary>
/// <param name="path">文件相对路径</param>
/// <returns></returns>
public
static
string
GetAliFileUrl
(
string
path
)
{
if
(
string
.
IsNullOrWhiteSpace
(
path
))
{
return
null
;
}
else
{
string
p
=
@"(http|https)://(?<domain>[^(:|/]*)"
;
Regex
reg
=
new
Regex
(
p
,
RegexOptions
.
IgnoreCase
);
string
value
=
reg
.
Match
(
path
).
Value
;
if
(!
string
.
IsNullOrEmpty
(
value
))
{
return
path
;
}
else
{
return
GetAliOssFileUrl
+
path
;
}
}
}
/// <summary>
/// 获取文件相对路径
/// </summary>
...
...
Mall.Repository/BaseSetUp/RB_File_StoreRepository.cs
View file @
59ad934e
...
...
@@ -31,7 +31,7 @@ namespace Mall.Repository.BaseSetUp
{
builder
.
Append
(
$" AND
{
nameof
(
RB_File_Store
.
TenantId
)}
=
{
query
.
TenantId
}
"
);
}
if
(
query
.
Tenant
Id
>
0
)
if
(
query
.
MallBase
Id
>
0
)
{
builder
.
Append
(
$" AND
{
nameof
(
RB_File_Store
.
MallBaseId
)}
=
{
query
.
MallBaseId
}
"
);
}
...
...
@@ -39,6 +39,10 @@ namespace Mall.Repository.BaseSetUp
{
builder
.
Append
(
$" AND
{
nameof
(
RB_File_Store
.
StoreType
)}
=
{(
int
)
query
.
StoreType
}
"
);
}
if
(
query
.
IsDefault
>
0
)
{
builder
.
Append
(
$" AND
{
nameof
(
RB_File_Store
.
IsDefault
)}
=
{
query
.
IsDefault
}
"
);
}
}
return
Get
<
RB_File_Store
>(
builder
.
ToString
()).
ToList
();
}
...
...
Mall.ThirdCore/Oss/AliOssService.cs
View file @
59ad934e
...
...
@@ -39,37 +39,37 @@ namespace Mall.ThirdCore.Oss
/// 创建实例
/// </summary>
/// <returns></returns>
public
static
AliOssService
Instance
()
public
static
AliOssService
Instance
(
string
endpoint
,
string
accessKeyId
,
string
accessKeySecret
,
string
bucketName
)
{
JObject
aliOss
=
Config
.
AliOss
;
string
endpoint
=
aliOss
[
"endpoint"
].
ToString
();
string
accessKeyId
=
aliOss
[
"accessKeyId"
].
ToString
();
string
accessKeySecret
=
aliOss
[
"accessKeySecret"
].
ToString
();
string
bucketName
=
aliOss
[
"bucketName"
].
ToString
();
endpoint
??
=
aliOss
[
"endpoint"
].
ToString
();
accessKeyId
??
=
aliOss
[
"accessKeyId"
].
ToString
();
accessKeySecret
??
=
aliOss
[
"accessKeySecret"
].
ToString
();
bucketName
??
=
aliOss
[
"bucketName"
].
ToString
();
return
new
AliOssService
(
endpoint
,
accessKeyId
,
accessKeySecret
,
bucketName
);
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="file
Name">文件名
</param>
/// <param name="file
Path">文件路径
</param>
/// <param name="stream">文件流</param>
/// <returns></returns>
public
OssResult
UploadFile
(
string
file
Name
,
System
.
IO
.
Stream
stream
)
public
OssResult
UploadFile
(
string
file
Path
,
System
.
IO
.
Stream
stream
)
{
byte
[]
bytes
=
new
byte
[
stream
.
Length
];
stream
.
Read
(
bytes
,
0
,
bytes
.
Length
);
OssClient
client
=
new
OssClient
(
this
.
endpoint
,
this
.
accessKeyId
,
this
.
accessKeySecret
);
OssResult
result
;
try
{
fileName
=
fileName
.
Substring
(
0
,
1
)
==
"/"
?
fileName
.
TrimStart
(
'/'
)
:
fileName
;
System
.
IO
.
MemoryStream
requestContent
=
new
System
.
IO
.
MemoryStream
(
bytes
);
client
.
PutObject
(
bucketName
,
fileName
,
requestContent
);
string
fileExtention
=
System
.
IO
.
Path
.
GetExtension
(
filePath
);
string
key
=
@"SaleBefore/Goods/"
+
DateTime
.
Now
.
Ticks
.
ToString
()
+
fileExtention
;
//对象在存储桶中的位置,即称对象键
client
.
PutObject
(
bucketName
,
key
,
filePath
);
result
=
new
OssResult
{
resultCode
=
1
resultCode
=
1
,
message
=
"上传成功"
,
data
=
"/"
+
key
};
}
catch
(
System
.
Exception
ex
)
...
...
Mall.ThirdCore/Oss/OssService.cs
View file @
59ad934e
...
...
@@ -8,9 +8,18 @@ namespace Mall.ThirdCore.Oss
/// 创建上传实例
/// </summary>
/// <returns></returns>
public
static
IOssService
Get
(
)
public
static
IOssService
Get
Tencent
(
string
endpoint
=
null
,
string
accessKeyId
=
null
,
string
accessKeySecret
=
null
,
string
bucketName
=
null
)
{
return
TencentOss
.
Instance
();
return
TencentOss
.
Instance
(
endpoint
,
accessKeyId
,
accessKeySecret
,
bucketName
);
}
/// <summary>
/// 创建上传实例
/// </summary>
/// <returns></returns>
public
static
IOssService
GetAli
(
string
endpoint
=
null
,
string
accessKeyId
=
null
,
string
accessKeySecret
=
null
,
string
bucketName
=
null
)
{
return
AliOssService
.
Instance
(
endpoint
,
accessKeyId
,
accessKeySecret
,
bucketName
);
}
}
}
Mall.ThirdCore/Oss/TencentOss.cs
View file @
59ad934e
...
...
@@ -40,12 +40,12 @@ namespace Mall.ThirdCore.Oss
/// 创建实例
/// </summary>
/// <returns></returns>
public
static
TencentOss
Instance
()
public
static
TencentOss
Instance
(
string
endpoint
,
string
accessKeyId
,
string
accessKeySecret
,
string
bucketName
)
{
string
endpoint
=
"ap-chengdu"
;
string
accessKeyId
=
"AKIDDPnbIzi8C1eqEOPP8dw6MNAg9H9ldDKd"
;
string
accessKeySecret
=
"PdcLtOjslUzNFYdU4OSI1fKtdHpFT2Ob"
;
string
bucketName
=
"viitto-1301420277"
;
endpoint
??
=
"ap-chengdu"
;
accessKeyId
??
=
"AKIDDPnbIzi8C1eqEOPP8dw6MNAg9H9ldDKd"
;
accessKeySecret
??
=
"PdcLtOjslUzNFYdU4OSI1fKtdHpFT2Ob"
;
bucketName
??
=
"viitto-1301420277"
;
return
new
TencentOss
(
endpoint
,
accessKeyId
,
accessKeySecret
,
bucketName
);
}
...
...
@@ -62,7 +62,7 @@ namespace Mall.ThirdCore.Oss
.
SetConnectionTimeoutMs
(
60000
)
//设置连接超时时间,单位毫秒,默认45000ms
.
SetReadWriteTimeoutMs
(
40000
)
//设置读写超时时间,单位毫秒,默认45000ms
.
IsHttps
(
true
)
//设置默认 HTTPS 请求
.
SetAppid
(
"1301420277"
)
//设置腾讯云账户的账户标识 APPID
//
.SetAppid("1301420277") //设置腾讯云账户的账户标识 APPID
.
SetRegion
(
this
.
endpoint
)
//设置一个默认的存储桶地域
.
Build
();
...
...
Mall.WebApi/Controllers/File/FileController.cs
View file @
59ad934e
...
...
@@ -9,6 +9,9 @@ using Mall.WebApi.Filter;
using
Mall.Common.API
;
using
Mall.Common.Plugin
;
using
Mall.ThirdCore.Oss
;
using
Mall.Module.BaseSetUp
;
using
Microsoft.AspNetCore.Http
;
using
Newtonsoft.Json.Linq
;
namespace
Mall.WebApi.Controllers.File
{
...
...
@@ -18,6 +21,8 @@ namespace Mall.WebApi.Controllers.File
[
EnableCors
(
"AllowCors"
)]
public
class
FileController
:
ControllerBase
{
MallBaseModule
mallBaseModule
=
new
MallBaseModule
();
/// <summary>
/// 上传文件到本地临时文件
/// </summary>
...
...
@@ -138,16 +143,59 @@ namespace Mall.WebApi.Controllers.File
{
files
[
0
].
CopyTo
(
fstream
);
}
IOssService
ossService
=
OssService
.
Get
();
OssResult
result
=
ossService
.
UploadFile
(
path_server
,
null
);
try
#
region
查询后台文件存储配置
string
q
=
Request
.
Query
[
"MallBaseId"
];
int
MallBaseId
=
Convert
.
ToInt32
(
q
==
""
?
"0"
:
q
);
bool
IsDefault
=
true
;
if
(
MallBaseId
>
0
)
{
System
.
IO
.
File
.
Delete
(
path_server
);
var
fileModel
=
mallBaseModule
.
GetFileStoreList
(
new
Model
.
Entity
.
BaseSetUp
.
RB_File_Store
{
MallBaseId
=
MallBaseId
,
IsDefault
=
1
}).
FirstOrDefault
();
if
(
fileModel
!=
null
)
{
if
(
fileModel
.
StoreType
==
Common
.
Enum
.
MallBase
.
StoreTypeEnum
.
Tencent
)
{
IsDefault
=
false
;
IOssService
ossService
=
OssService
.
GetTencent
(
fileModel
.
Region
,
fileModel
.
SecretId
,
fileModel
.
SecretKey
,
fileModel
.
Bucket
);
OssResult
result
=
ossService
.
UploadFile
(
path_server
,
null
);
try
{
System
.
IO
.
File
.
Delete
(
path_server
);
}
catch
{
}
return
ApiResult
.
Success
(
""
,
Common
.
Config
.
GetFileUrl
(
result
.
data
.
ToString
()));
}
else
if
(
fileModel
.
StoreType
==
Common
.
Enum
.
MallBase
.
StoreTypeEnum
.
Alicloud
)
{
IsDefault
=
false
;
IOssService
ossService
=
OssService
.
GetAli
(
fileModel
.
Region
,
fileModel
.
SecretId
,
fileModel
.
SecretKey
,
fileModel
.
Bucket
);
OssResult
result
=
ossService
.
UploadFile
(
path_server
,
null
);
try
{
System
.
IO
.
File
.
Delete
(
path_server
);
}
catch
{
}
return
ApiResult
.
Success
(
""
,
Common
.
Config
.
GetAliFileUrl
(
result
.
data
.
ToString
()));
}
}
}
catch
{
#
endregion
if
(
IsDefault
)
{
IOssService
ossService
=
OssService
.
GetTencent
();
OssResult
result
=
ossService
.
UploadFile
(
path_server
,
null
);
try
{
System
.
IO
.
File
.
Delete
(
path_server
);
}
catch
{
}
return
ApiResult
.
Success
(
""
,
Common
.
Config
.
GetFileUrl
(
result
.
data
.
ToString
()));
}
return
ApiResult
.
Success
(
""
,
Common
.
Config
.
GetFileUrl
(
result
.
data
.
ToString
())
);
return
ApiResult
.
Failed
(
);
}
catch
(
Exception
ex
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment