[代码]FileAction.java
|
net.oschina.action;
|
002
|
003
|
import java.io.*;
|
004
|
005
|
import org.apache.commons.io.FilenameUtils;
|
006
|
import org.apache.commons.io.IOUtils;
|
007
|
import org.apache.commons.lang.StringUtils;
|
008
|
009
|
import net.oschina.beans.File;
|
010
|
import net.oschina.beans.User;
|
011
|
import net.oschina.service.StorageService;
|
012
|
import net.oschina.toolbox.LinkTool;
|
013
|
import my.mvc.Annotation;
|
014
|
import my.mvc.RequestContext;
|
015
|
import my.util.Multimedia;
|
016
|
017
|
/**
|
018
|
*
文件库
|
019
|
*
@author Winter Lau
|
020
|
*
@date 2010-7-4 下午01:35:12
|
021
|
*/
|
022
|
public class FileAction
{
|
023
|
|
024
|
public final static long MAX_FILE_SIZE
= 2 * 1024 * 1024 ;
|
025
|
026
|
/**
|
027
|
*
文件下载
|
028
|
*
@param ctx
|
029
|
*
@throws IOException
|
030
|
*/
|
031
|
public void download(RequestContext
ctx) throws IOException
{
|
032
|
if (ctx.isRobot()){
|
033
|
ctx.forbidden();
|
034
|
return ;
|
035
|
}
|
036
|
|
037
|
long id
= ctx.id();
|
038
|
File
bean = File.INSTANCE.Get(id);
|
039
|
if (bean
== null ){
|
040
|
ctx.not_found();
|
041
|
return ;
|
042
|
}
|
043
|
044
|
String
f_ident = ctx.param( "fn" , "" );
|
045
|
if (id>= 100 &&
!StringUtils.equals(f_ident, bean.getIdent())){
|
046
|
ctx.not_found();
|
047
|
return ;
|
048
|
}
|
049
|
|
050
|
//
检查下载权限
|
051
|
if (bean.IsLoginRequired()
&& ctx.user()== null ){
|
052
|
StringBuilder
login = new StringBuilder(LinkTool.home( "home/login?goto_page=" ));
|
053
|
login.append(LinkTool.encode_url(LinkTool.action( "file/download?id=" +bean.getId()+ "&fn=" +bean.getIdent())));
|
054
|
ctx.redirect(login.toString());
|
055
|
return ;
|
056
|
}
|
057
|
|
058
|
FileInputStream
fis = null ;
|
059
|
try {
|
060
|
java.io.File
file = StorageService.FILES.readFile(bean.getPath());
|
061
|
fis
= new FileInputStream(file);
|
062
|
//设置
content-type
|
063
|
ctx.response().setContentLength(( int )file.length());
|
064
|
String
ext = FilenameUtils.getExtension(bean.getPath());
|
065
|
String
mine_type = Multimedia.mime_types.get(ext);
|
066
|
if (mine_type
!= null )
|
067
|
ctx.response().setContentType(mine_type);
|
068
|
String
ua = ctx.header( "user-agent" );
|
069
|
if (ua
!= null &&
ua.indexOf( "Firefox" )>= 0 )
|
070
|
ctx.header( "Content-Disposition" , "attachment;
filename*=\"utf8''" +
|
071
|
LinkTool.encode_url(bean.getName())+ "." +ext+ "\"" );
|
072
|
else
|
073
|
ctx.header( "Content-Disposition" , "attachment;
filename=" +
|
074
|
LinkTool.encode_url(bean.getName()+ "." +ext));
|
075
|
IOUtils.copy(fis,
ctx.response().getOutputStream());
|
076
|
bean.IncDownloadCount( 1 );
|
077
|
//}catch(FileNotFoundException
e){
|
078
|
//
ctx.not_found();
|
079
|
} finally {
|
080
|
IOUtils.closeQuietly(fis);
|
081
|
}
|
082
|
}
|
083
|
|
084
|
/**
|
085
|
*
文件上传
|
086
|
*
@param ctx
|
087
|
*
@throws IOException
|
088
|
*/
|
089
|
@Annotation .UserRoleRequired(role=User.ROLE_EDITOR)
|
090
|
public void upload(RequestContext
ctx) throws IOException
{
|
091
|
File
form = ctx.form(File. class );
|
092
|
if (StringUtils.isBlank(form.getName()))
|
093
|
throw ctx.error( "file_name_empty" );
|
094
|
java.io.File
file = ctx.file( "file" );
|
095
|
if (file
== null )
|
096
|
throw ctx.error( "file_empty" );
|
097
|
if (!File.IsLegalFile(file.getName()))
|
098
|
throw ctx.error( "file_illegal" );
|
099
|
String
the_path = form.getUrl();
|
100
|
//判断文件是否存在
|
101
|
if (StringUtils.isNotBlank(the_path)
&& StorageService.FILES.exist(the_path))
|
102
|
throw ctx.error( "file_exists" ,
the_path);
|
103
|
String
uri = StringUtils.isBlank(the_path)?
|
104
|
StorageService.FILES.save(file):StorageService.FILES.save(file,the_path); //文件存储
|
105
|
form.setSize(file.length());
|
106
|
form.setUrl(uri);
|
107
|
form.setPath(uri);
|
108
|
form.setUser(ctx.user().getId());
|
109
|
form.setDl_count( 0 );
|
110
|
form.Save();
|
111
|
throw ctx.error( "file_upload_ok" ,
|
112
|
LinkTool.action( "file/download?id=" +form.getId()+ "&fn=" +form.getIdent()));
|
113
|
}
|
114
|
115
|
/**
|
116
|
*
文件修改
|
117
|
*
@param ctx
|
118
|
*
@throws IOException
|
119
|
*/
|
120
|
@Annotation .UserRoleRequired(role=User.ROLE_EDITOR)
|
121
|
public void edit(RequestContext
ctx) throws IOException
{
|
122
|
File
form = ctx.form(File. class );
|
123
|
if (StringUtils.isBlank(form.getName()))
|
124
|
throw ctx.error( "file_name_empty" );
|
125
|
File
bean = File.INSTANCE.Get(form.getId());
|
126
|
java.io.File
pic = ctx.file( "file" );
|
127
|
if (pic
!= null ){
|
128
|
if (!File.IsLegalFile(pic.getName()))
|
129
|
throw ctx.error( "file_illegal" );
|
130
|
bean.setSize(pic.length());
|
131
|
StorageService.FILES.save(pic,
bean.getPath()); //文件存储
|
132
|
}
|
133
|
bean.setName(form.getName());
|
134
|
bean.setDetail(form.getDetail());
|
135
|
bean.setOptions(form.getOptions());
|
136
|
bean.Update();
|
137
|
throw ctx.error( "file_edit_ok" );
|
138
|
}
|
139
|
140
|
/**
|
141
|
*
文件删除
|
142
|
*
@param ctx
|
143
|
*
@throws IOException
|
144
|
*/
|
145
|
@Annotation .UserRoleRequired(role=User.ROLE_ADMIN)
|
146
|
public void delete(RequestContext
ctx) throws IOException
{
|
147
|
long id
= ctx.id();
|
148
|
File
file = File.INSTANCE.Get(id);
|
149
|
if (file
!= null ){
|
150
|
StorageService.FILES.delete(file.getPath());
|
151
|
file.Delete();
|
152
|
}
|
153
|
}
|
154
|
155
|
}
|