Extract media-related functions from the notepad page into the notepad module.
This commit is contained in:
parent
44319b1a7f
commit
ae6c9b553e
|
@ -9,17 +9,17 @@
|
|||
notepad-update-note notepad-delete-note
|
||||
; Media operations
|
||||
notepad-list-media notepad-has-media?
|
||||
notepad-media-type notepad-new-media
|
||||
notepad-move-media notepad-delete-media
|
||||
; User operations
|
||||
|
||||
; Format a note
|
||||
notepad-format
|
||||
; Remove later
|
||||
notepad-notes-dir
|
||||
notepad-media-dir
|
||||
)
|
||||
|
||||
(require
|
||||
racket/port
|
||||
racket/string
|
||||
"notes.rkt"
|
||||
"user.rkt"
|
||||
"stored-objects.rkt"
|
||||
|
@ -201,3 +201,34 @@
|
|||
; Does the notepad has a given media ?
|
||||
(define (notepad-has-media? np media)
|
||||
(and media (file-exists? (string-append (notepad-media-dir np) "/" media))))
|
||||
|
||||
; Get the type of media
|
||||
; Return 'image, 'other or #f
|
||||
(define (notepad-media-type np media)
|
||||
(cond
|
||||
( (ormap (lambda (x) (string-suffix? media x)) '(".png" ".jpg" ".gif" ".jpeg" ".svg"))
|
||||
'image)
|
||||
( #t
|
||||
'other)))
|
||||
|
||||
; Save a media
|
||||
; Takes a filename and an input stream
|
||||
(define (notepad-new-media np filename in)
|
||||
(define filepath (string-append (notepad-media-dir np) "/" filename))
|
||||
(call-with-output-file
|
||||
filepath
|
||||
(lambda (out)
|
||||
(copy-port in out)
|
||||
(close-input-port in))
|
||||
#:exists 'truncate/replace))
|
||||
|
||||
; Move a media
|
||||
(define (notepad-move-media np media filename)
|
||||
(define media-dir (notepad-media-dir np))
|
||||
(rename-file-or-directory
|
||||
(string-append media-dir "/" media)
|
||||
(string-append media-dir "/" filename)))
|
||||
|
||||
; Remove a media
|
||||
(define (notepad-delete-media np media)
|
||||
(delete-file (string-append (notepad-media-dir np) "/" media)))
|
||||
|
|
|
@ -38,10 +38,6 @@
|
|||
(define dev? #t)
|
||||
; Notepad
|
||||
(define notepad (make-notepad notepad-dir #:dev? dev?))
|
||||
; Notes directory
|
||||
(define notes-dir (notepad-notes-dir notepad))
|
||||
; Media directory
|
||||
(define media-dir (notepad-media-dir notepad))
|
||||
|
||||
; Secured : either protocol is https or dev mode is active
|
||||
(define (check-secured? param)
|
||||
|
@ -263,20 +259,19 @@
|
|||
(define secured? (check-secured? param))
|
||||
(define can-edit? (and connected-usr secured?))
|
||||
(define media (weblet-parameter-ref param 'media #f))
|
||||
(define file (and media (string-append media-dir "/" media)))
|
||||
(define has-media? (file-exists? file))
|
||||
(define image? (ormap (lambda (x) (string-suffix? media x)) '(".png" ".jpg" ".gif" ".jpeg" ".svg")))
|
||||
(define direct-link (string-append "/media/get/" media))
|
||||
(cond
|
||||
(has-media?
|
||||
( (notepad-has-media? notepad media)
|
||||
( (pages:template
|
||||
#:title (string-append "Fichier: " media)
|
||||
#:author "feuforeve.fr"
|
||||
#:content
|
||||
`(article
|
||||
,@(if image?
|
||||
`((img ((src ,direct-link)))(br))
|
||||
'())
|
||||
,@(case (notepad-media-type notepad media)
|
||||
( (image)
|
||||
`((a ((href ,direct-link)) (img ((src ,direct-link)(class "image-preview"))))(br)))
|
||||
( else
|
||||
'()))
|
||||
(a ((href ,direct-link)) "Lien vers le fichier")
|
||||
,@(if can-edit?
|
||||
`((hr)
|
||||
|
@ -325,16 +320,10 @@
|
|||
( (and connected-usr secured? (eq? method 'post))
|
||||
(define filename (weblet-parameter-ref param 'filename #f))
|
||||
(define in (and filename (weblet-parameter-file-port-ref param 'filename)))
|
||||
(define filepath (and filename in (string-append media-dir "/" filename)))
|
||||
; Save file
|
||||
(cond
|
||||
( filepath
|
||||
(call-with-output-file
|
||||
filepath
|
||||
(lambda (out)
|
||||
(copy-port in out)
|
||||
(close-input-port in))
|
||||
#:exists 'truncate/replace)
|
||||
( (and filename in)
|
||||
(notepad-new-media notepad filename in)
|
||||
; Redirect
|
||||
(redirect-to
|
||||
(string-append "/media/show/" filename)
|
||||
|
@ -357,10 +346,7 @@
|
|||
(define secured? (check-secured? param))
|
||||
(define method (weblet-parameter-method param))
|
||||
(define media (weblet-parameter-ref param 'media #f))
|
||||
(define file (and media (string-append media-dir "/" media)))
|
||||
(define has-media? (file-exists? file))
|
||||
(define image? (ormap (lambda (x) (string-suffix? media x)) '(".png" ".jpg" ".gif" ".jpeg" ".svg")))
|
||||
(define direct-link (string-append "/media/get/" media))
|
||||
(define has-media? (notepad-has-media? notepad media))
|
||||
(define failed? (equal? "t" (weblet-parameter-ref param 'error #f)))
|
||||
(cond
|
||||
( (and has-media? connected-usr secured? (eq? method 'get))
|
||||
|
@ -387,15 +373,13 @@
|
|||
(cond
|
||||
( (and filename (not (equal? filename "")) (not (equal? filename media)))
|
||||
; Move file
|
||||
(rename-file-or-directory
|
||||
file
|
||||
(string-append media-dir "/" filename))
|
||||
(notepad-move-media notepad media filename)
|
||||
; Redirect
|
||||
(redirect-to
|
||||
(string-append "/media/show/" filename)
|
||||
see-other))
|
||||
( #t
|
||||
; Redirect: upload failed
|
||||
; Redirect: operation failed
|
||||
(redirect-to
|
||||
(string-append "/media/edit/" media "?error=t")
|
||||
see-other))))
|
||||
|
@ -416,8 +400,7 @@
|
|||
(define secured? (check-secured? param))
|
||||
(define method (weblet-parameter-method param))
|
||||
(define media (weblet-parameter-ref param 'media #f))
|
||||
(define file (and media (string-append media-dir "/" media)))
|
||||
(define has-media? (file-exists? file))
|
||||
(define has-media? (notepad-has-media? notepad media))
|
||||
(cond
|
||||
( (and has-media? connected-usr secured? (eq? method 'get))
|
||||
; Method get => ask for confirmation
|
||||
|
@ -438,7 +421,7 @@
|
|||
param))
|
||||
( (and has-media? connected-usr secured? (eq? method 'post))
|
||||
; Method post => remove
|
||||
(delete-file file)
|
||||
(notepad-delete-media notepad media)
|
||||
(redirect-to
|
||||
"/media/list"
|
||||
see-other))
|
||||
|
|
Loading…
Reference in New Issue