@@ -103,13 +103,62 @@ func renderDirectory(ctx *context.Context, treeLink string) {
103
103
isTextFile := base .IsTextFile (buf )
104
104
ctx .Data ["FileIsText" ] = isTextFile
105
105
ctx .Data ["FileName" ] = readmeFile .Name ()
106
+ fileSize := int64 (0 )
107
+ isLFSFile := false
108
+ ctx .Data ["IsLFSFile" ] = false
109
+
106
110
// FIXME: what happens when README file is an image?
111
+ if isTextFile && setting .LFS .StartServer {
112
+ meta := lfs .IsPointerFile (& buf )
113
+ if meta != nil {
114
+ meta , err = ctx .Repo .Repository .GetLFSMetaObjectByOid (meta .Oid )
115
+ if err != nil && err != models .ErrLFSObjectNotExist {
116
+ ctx .ServerError ("GetLFSMetaObject" , err )
117
+ return
118
+ }
119
+ }
120
+
121
+ if meta != nil {
122
+ ctx .Data ["IsLFSFile" ] = true
123
+ isLFSFile = true
124
+
125
+ // OK read the lfs object
126
+ var err error
127
+ dataRc , err = lfs .ReadMetaObject (meta )
128
+ if err != nil {
129
+ ctx .ServerError ("ReadMetaObject" , err )
130
+ return
131
+ }
132
+ defer dataRc .Close ()
133
+
134
+ buf = make ([]byte , 1024 )
135
+ n , err = dataRc .Read (buf )
136
+ if err != nil {
137
+ ctx .ServerError ("Data" , err )
138
+ return
139
+ }
140
+ buf = buf [:n ]
141
+
142
+ isTextFile = base .IsTextFile (buf )
143
+ ctx .Data ["IsTextFile" ] = isTextFile
144
+
145
+ fileSize = meta .Size
146
+ ctx .Data ["FileSize" ] = meta .Size
147
+ filenameBase64 := base64 .RawURLEncoding .EncodeToString ([]byte (readmeFile .Name ()))
148
+ ctx .Data ["RawFileLink" ] = fmt .Sprintf ("%s%s.git/info/lfs/objects/%s/%s" , setting .AppURL , ctx .Repo .Repository .FullName (), meta .Oid , filenameBase64 )
149
+ }
150
+ }
151
+
152
+ if ! isLFSFile {
153
+ fileSize = readmeFile .Size ()
154
+ }
155
+
107
156
if isTextFile {
108
- if readmeFile . Size () >= setting .UI .MaxDisplayFileSize {
157
+ if fileSize >= setting .UI .MaxDisplayFileSize {
109
158
// Pretend that this is a normal text file to display 'This file is too large to be shown'
110
159
ctx .Data ["IsFileTooLarge" ] = true
111
160
ctx .Data ["IsTextFile" ] = true
112
- ctx .Data ["FileSize" ] = readmeFile . Size ()
161
+ ctx .Data ["FileSize" ] = fileSize
113
162
} else {
114
163
d , _ := ioutil .ReadAll (dataRc )
115
164
buf = templates .ToUTF8WithFallback (append (buf , d ... ))
@@ -168,7 +217,8 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
168
217
169
218
ctx .Data ["Title" ] = ctx .Data ["Title" ].(string ) + " - " + ctx .Repo .TreePath + " at " + ctx .Repo .BranchName
170
219
171
- ctx .Data ["FileSize" ] = blob .Size ()
220
+ fileSize := blob .Size ()
221
+ ctx .Data ["FileSize" ] = fileSize
172
222
ctx .Data ["FileName" ] = blob .Name ()
173
223
ctx .Data ["HighlightClass" ] = highlight .FileNameToHighlightClass (blob .Name ())
174
224
ctx .Data ["RawFileLink" ] = rawLink + "/" + ctx .Repo .TreePath
@@ -182,31 +232,43 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
182
232
ctx .Data ["IsTextFile" ] = isTextFile
183
233
184
234
//Check for LFS meta file
185
- if isTextFile {
186
- if meta := lfs .IsPointerFile (& buf ); meta != nil {
187
- if meta , _ = ctx .Repo .Repository .GetLFSMetaObjectByOid (meta .Oid ); meta != nil {
188
- ctx .Data ["IsLFSFile" ] = true
189
- isLFSFile = true
190
-
191
- // OK read the lfs object
192
- dataRc , err := lfs .ReadMetaObject (meta )
193
- if err != nil {
194
- ctx .ServerError ("ReadMetaObject" , err )
195
- return
196
- }
197
- defer dataRc .Close ()
235
+ if isTextFile && setting .LFS .StartServer {
236
+ meta := lfs .IsPointerFile (& buf )
237
+ if meta != nil {
238
+ meta , err = ctx .Repo .Repository .GetLFSMetaObjectByOid (meta .Oid )
239
+ if err != nil && err != models .ErrLFSObjectNotExist {
240
+ ctx .ServerError ("GetLFSMetaObject" , err )
241
+ return
242
+ }
243
+ }
244
+ if meta != nil {
245
+ ctx .Data ["IsLFSFile" ] = true
246
+ isLFSFile = true
247
+
248
+ // OK read the lfs object
249
+ var err error
250
+ dataRc , err = lfs .ReadMetaObject (meta )
251
+ if err != nil {
252
+ ctx .ServerError ("ReadMetaObject" , err )
253
+ return
254
+ }
255
+ defer dataRc .Close ()
198
256
199
- buf = make ([]byte , 1024 )
200
- n , _ = dataRc .Read (buf )
201
- buf = buf [:n ]
257
+ buf = make ([]byte , 1024 )
258
+ n , err = dataRc .Read (buf )
259
+ if err != nil {
260
+ ctx .ServerError ("Data" , err )
261
+ return
262
+ }
263
+ buf = buf [:n ]
202
264
203
- isTextFile = base .IsTextFile (buf )
204
- ctx .Data ["IsTextFile" ] = isTextFile
265
+ isTextFile = base .IsTextFile (buf )
266
+ ctx .Data ["IsTextFile" ] = isTextFile
205
267
206
- ctx . Data [ "FileSize" ] = meta .Size
207
- filenameBase64 := base64 . RawURLEncoding . EncodeToString ([] byte ( blob . Name ()))
208
- ctx . Data [ "RawFileLink" ] = fmt . Sprintf ( "%s%s.git/info/lfs/objects/%s/%s" , setting . AppURL , ctx . Repo . Repository . FullName (), meta . Oid , filenameBase64 )
209
- }
268
+ fileSize = meta .Size
269
+ ctx . Data [ "FileSize" ] = meta . Size
270
+ filenameBase64 := base64 . RawURLEncoding . EncodeToString ([] byte ( blob . Name ()) )
271
+ ctx . Data [ "RawFileLink" ] = fmt . Sprintf ( "%s%s.git/info/lfs/objects/%s/%s" , setting . AppURL , ctx . Repo . Repository . FullName (), meta . Oid , filenameBase64 )
210
272
}
211
273
}
212
274
@@ -219,7 +281,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
219
281
220
282
switch {
221
283
case isTextFile :
222
- if blob . Size () >= setting .UI .MaxDisplayFileSize {
284
+ if fileSize >= setting .UI .MaxDisplayFileSize {
223
285
ctx .Data ["IsFileTooLarge" ] = true
224
286
break
225
287
}
0 commit comments