Skip to content

Commit 2ceb5f3

Browse files
author
Charles-Antoine Mathieu
authored
Merge pull request src-d#219 from camathieu/1.2.1
1.2.1
2 parents 436ca47 + 29b194b commit 2ceb5f3

File tree

15 files changed

+767
-230
lines changed

15 files changed

+767
-230
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ language: go
33
sudo: required
44

55
go:
6-
- 1.7.4
6+
- 1.9.1
77

88
before_install:
99
# Update node_js version : https://github.com/travis-ci/travis-ci/issues/7108

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# THE SOFTWARE.
2525
###
2626

27-
RELEASE_VERSION="1.2"
27+
RELEASE_VERSION="1.2.1"
2828
RELEASE_DIR="release/plik-$(RELEASE_VERSION)"
2929
RELEASE_TARGETS=darwin-386 darwin-amd64 freebsd-386 \
3030
freebsd-amd64 linux-386 linux-amd64 linux-arm openbsd-386 \

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ Plik is a scalable & friendly temporary file upload system ( wetransfer like ) i
2020
- Upload restriction : Source IP / Token
2121

2222
### Version
23-
1.2
23+
1.2.1
2424

2525
### Installation
2626

2727
##### From release
2828
To run plik, it's very simple :
2929
```sh
30-
$ wget https://github.com/root-gg/plik/releases/download/1.2/plik-1.2-linux-64bits.tar.gz
31-
$ tar xzvf plik-1.2-linux-64bits.tar.gz
32-
$ cd plik-1.2/server
30+
$ wget https://github.com/root-gg/plik/releases/download/1.2.1/plik-1.2.1-linux-64bits.tar.gz
31+
$ tar xzvf plik-1.2.1-linux-64bits.tar.gz
32+
$ cd plik-1.2.1/server
3333
$ ./plikd
3434
```
3535
Et voilà ! You now have a fully functional instance of plik running on http://127.0.0.1:8080.

changelog/1.2.1

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Hi,
2+
3+
We're happy to release Plik 1.2.1
4+
5+
Here is the changelog :
6+
7+
New :
8+
- Configuration option to prevent anonymous uploads.
9+
- Configuration option to limit google email domains
10+
- Serve HTTP on a different root path
11+
- Display release notes on client update
12+
- Backend testing script using Docker
13+
- Load client config from /etc/plik/plikrc
14+
15+
Misc :
16+
- Add mongodb connect timeout
17+
- Migrating from godeps to govendor
18+
- Enable race detector when testing
19+
- Add a confirm dialog to delete an Upload
20+
- update golang and node_js version in Travis
21+
- Update npm / bower dependencies ( bump Angularjs from 1.2 to 1.6 )
22+
23+
Fix :
24+
- Fix uploaded file size calculus
25+
- Don't log file upload request body in debug mode
26+
- Clean url properly when deleting upload
27+
- Fix NPD in swift authentication
28+
- Add missing letters to randRunes
29+
- add clean-frontend to clean-all makefile directive
30+
- Webapp : Fix progress bar when uploading file fails
31+
32+
The binaires are compiled with go 1.9.1.
33+
34+
Faithfully,
35+
The Plik Team.

client/plik.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,14 @@ func upload(uploadInfo *common.Upload, fileToUpload *config.FileToUpload, reader
294294
fmt.Printf("%s\n", getFileCommand(uploadInfo, fileToUpload.File))
295295
}
296296

297-
// TODO Handler error properly here
298-
go func() error {
299-
297+
errCh := make(chan error)
298+
go func(errCh chan error) {
300299
part, err := multipartWriter.CreateFormFile("file", fileToUpload.Name)
301300
if err != nil {
302-
fmt.Println(err)
303-
return pipeWriter.CloseWithError(err)
301+
err = fmt.Errorf("Unable to create multipartWriter : %s", err)
302+
pipeWriter.CloseWithError(err)
303+
errCh <- err
304+
return
304305
}
305306

306307
var multiWriter io.Writer
@@ -322,20 +323,27 @@ func upload(uploadInfo *common.Upload, fileToUpload *config.FileToUpload, reader
322323
if config.Config.Secure {
323324
err = config.GetCryptoBackend().Encrypt(reader, multiWriter)
324325
if err != nil {
325-
fmt.Println(err)
326-
return pipeWriter.CloseWithError(err)
326+
pipeWriter.CloseWithError(err)
327+
errCh <- err
328+
return
327329
}
328330
} else {
329331
_, err = io.Copy(multiWriter, reader)
330332
if err != nil {
331-
fmt.Println(err)
332-
return pipeWriter.CloseWithError(err)
333+
pipeWriter.CloseWithError(err)
334+
errCh <- err
335+
return
333336
}
334337
}
335338

336339
err = multipartWriter.Close()
337-
return pipeWriter.CloseWithError(err)
338-
}()
340+
if err != nil {
341+
err = fmt.Errorf("Unable to close multipartWriter : %s", err)
342+
}
343+
344+
pipeWriter.CloseWithError(err)
345+
errCh <- err
346+
}(errCh)
339347

340348
mode := "file"
341349
if uploadInfo.Stream {
@@ -345,13 +353,13 @@ func upload(uploadInfo *common.Upload, fileToUpload *config.FileToUpload, reader
345353
var URL *url.URL
346354
URL, err = url.Parse(config.Config.URL + "/" + mode + "/" + uploadInfo.ID + "/" + fileToUpload.ID + "/" + fileToUpload.Name)
347355
if err != nil {
348-
return
356+
return nil, err
349357
}
350358

351359
var req *http.Request
352360
req, err = http.NewRequest("POST", URL.String(), pipeReader)
353361
if err != nil {
354-
return
362+
return nil, err
355363
}
356364

357365
req.Header.Set("Content-Type", multipartWriter.FormDataContentType())
@@ -363,24 +371,30 @@ func upload(uploadInfo *common.Upload, fileToUpload *config.FileToUpload, reader
363371

364372
resp, err := makeRequest(req)
365373
if err != nil {
366-
return
374+
return nil, err
375+
}
376+
377+
err = <-errCh
378+
if err != nil {
379+
return nil, err
367380
}
368381

369382
defer resp.Body.Close()
370383
body, err := ioutil.ReadAll(resp.Body)
371384
if err != nil {
372-
return
385+
return nil, err
373386
}
374387

375388
// Parse Json response
376389
file = new(common.File)
377390
err = json.Unmarshal(body, file)
378391
if err != nil {
379-
return
392+
return nil, err
380393
}
381394

382395
config.Debug(fmt.Sprintf("Uploaded %s : %s", file.Name, config.Sdump(file)))
383-
return
396+
397+
return file, nil
384398
}
385399

386400
func getFileCommand(upload *common.Upload, file *common.File) (command string) {

server/handlers/addFile.go

Lines changed: 94 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ import (
4444
"github.com/root-gg/utils"
4545
)
4646

47+
type preprocessOutputReturn struct {
48+
size int64
49+
md5sum string
50+
mimeType string
51+
err error
52+
}
53+
4754
// AddFile add a file to an existing upload.
4855
func AddFile(ctx *juliet.Context, resp http.ResponseWriter, req *http.Request) {
4956
log := common.GetLogger(ctx)
@@ -105,6 +112,7 @@ func AddFile(ctx *juliet.Context, resp http.ResponseWriter, req *http.Request) {
105112
prefix := fmt.Sprintf("%s[%s]", log.Prefix, newFile.ID)
106113
log.SetPrefix(prefix)
107114

115+
// Save file to the context
108116
ctx.Set("file", newFile)
109117

110118
// Get file handle from multipart request
@@ -153,47 +161,11 @@ func AddFile(ctx *juliet.Context, resp http.ResponseWriter, req *http.Request) {
153161

154162
// Pipe file data from the request body to a preprocessing goroutine
155163
// - Guess content type
164+
// - Compute/Limit upload size
156165
// - Compute md5sum
157-
// - Limit upload size
158166
preprocessReader, preprocessWriter := io.Pipe()
159-
md5Hash := md5.New()
160-
totalBytes := 0
161-
go func() {
162-
for {
163-
buf := make([]byte, 1024)
164-
bytesRead, err := file.Read(buf)
165-
if err != nil {
166-
if err != io.EOF {
167-
log.Warningf("Unable to read data from request body : %s", err)
168-
}
169-
170-
preprocessWriter.Close()
171-
return
172-
}
173-
174-
// Detect the content-type using the 512 first bytes
175-
if totalBytes == 0 {
176-
newFile.Type = http.DetectContentType(buf)
177-
}
178-
179-
// Increment size
180-
totalBytes += bytesRead
181-
182-
// Compute md5sum
183-
md5Hash.Write(buf[:bytesRead])
184-
185-
// Check upload max size limit
186-
if int64(totalBytes) > common.Config.MaxFileSize {
187-
err = fmt.Errorf("File too big (limit is set to %d bytes)", common.Config.MaxFileSize)
188-
log.Warning(err.Error())
189-
preprocessWriter.CloseWithError(err)
190-
return
191-
}
192-
193-
// Pass file data to data backend
194-
preprocessWriter.Write(buf[:bytesRead])
195-
}
196-
}()
167+
preprocessOutputCh := make(chan preprocessOutputReturn)
168+
go preprocessor(ctx, file, preprocessWriter, preprocessOutputCh)
197169

198170
// Save file in the data backend
199171
var backend dataBackend.DataBackend
@@ -209,14 +181,24 @@ func AddFile(ctx *juliet.Context, resp http.ResponseWriter, req *http.Request) {
209181
return
210182
}
211183

184+
// Get preprocessor goroutine output
185+
preprocessOutput := <-preprocessOutputCh
186+
if preprocessOutput.err != nil {
187+
log.Warningf("Unable to execute preprocessor : %s", err)
188+
common.Fail(ctx, req, resp, "Unable to save file", 500)
189+
return
190+
}
191+
212192
// Fill-in file information
213-
newFile.CurrentSize = int64(totalBytes)
193+
newFile.Type = preprocessOutput.mimeType
194+
newFile.CurrentSize = preprocessOutput.size
195+
newFile.Md5 = preprocessOutput.md5sum
196+
214197
if upload.Stream {
215198
newFile.Status = "downloaded"
216199
} else {
217200
newFile.Status = "uploaded"
218201
}
219-
newFile.Md5 = fmt.Sprintf("%x", md5Hash.Sum(nil))
220202
newFile.UploadDate = time.Now().Unix()
221203
newFile.BackendDetails = backendDetails
222204

@@ -243,3 +225,74 @@ func AddFile(ctx *juliet.Context, resp http.ResponseWriter, req *http.Request) {
243225
return
244226
}
245227
}
228+
229+
// - Guess content type
230+
// - Compute/Limit upload size
231+
// - Compute md5sum
232+
func preprocessor(ctx *juliet.Context, file io.Reader, preprocessWriter io.WriteCloser, outputCh chan preprocessOutputReturn) {
233+
log := common.GetLogger(ctx)
234+
235+
var err error
236+
var totalBytes int64
237+
var mimeType string
238+
var md5sum string
239+
240+
md5Hash := md5.New()
241+
buf := make([]byte, 1048)
242+
243+
eof := false
244+
for !eof {
245+
bytesRead, err := file.Read(buf)
246+
if err == io.EOF {
247+
eof = true
248+
if bytesRead <= 0 {
249+
break
250+
}
251+
} else if err != nil {
252+
err = log.EWarningf("Unable to read data from request body : %s", err)
253+
break
254+
}
255+
256+
// Detect the content-type using the 512 first bytes
257+
if totalBytes == 0 {
258+
mimeType = http.DetectContentType(buf)
259+
}
260+
261+
// Increment size
262+
totalBytes += int64(bytesRead)
263+
264+
// Check upload max size limit
265+
if int64(totalBytes) > common.Config.MaxFileSize {
266+
err = log.EWarningf("File too big (limit is set to %d bytes)", common.Config.MaxFileSize)
267+
break
268+
}
269+
270+
// Compute md5sum
271+
md5Hash.Write(buf[:bytesRead])
272+
273+
// Forward data to the data backend
274+
bytesWritten, err := preprocessWriter.Write(buf[:bytesRead])
275+
if err != nil {
276+
log.Warning(err.Error())
277+
break
278+
}
279+
if bytesWritten != bytesRead {
280+
err = log.EWarningf("Invalid number of bytes written. Expected %d but got %d", bytesRead, bytesWritten)
281+
break
282+
}
283+
}
284+
285+
err = preprocessWriter.Close()
286+
if err != nil {
287+
log.Warningf("Unable to close preprocessWriter : %s", err)
288+
}
289+
290+
if err != nil {
291+
outputCh <- preprocessOutputReturn{err: err}
292+
} else {
293+
md5sum = fmt.Sprintf("%x", md5Hash.Sum(nil))
294+
outputCh <- preprocessOutputReturn{size: totalBytes, md5sum: md5sum, mimeType: mimeType}
295+
}
296+
297+
close(outputCh)
298+
}

server/metadataBackend/mongo/mongo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func NewMongoMetadataBackend(config map[string]interface{}) (mmb *MetadataBacken
6262
dialInfo := &mgo.DialInfo{}
6363
dialInfo.Addrs = []string{mmb.config.URL}
6464
dialInfo.Database = mmb.config.Database
65+
dialInfo.Timeout = 5 * time.Second
6566
if mmb.config.Username != "" && mmb.config.Password != "" {
6667
dialInfo.Username = mmb.config.Username
6768
dialInfo.Password = mmb.config.Password

server/vendor/gopkg.in/mgo.v2/Makefile

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)